/// <summary> /// Uses reflection to dynamically invoke a method, /// throwing an exception if it is not /// implemented on the target object. /// </summary> /// <param name="obj"> /// Object containing method. /// </param> /// <param name="method"> /// Name of the method. /// </param> /// <param name="parameters"> /// Parameters to pass to method. /// </param> public static object CallMethod(object obj, string method, params object[] parameters) { Guard.ArgumentNotNull(obj, "obj"); Guard.ArgumentNotNullOrEmpty(method, "method"); IDynamicMethod dynamicMethod = DynamicMethodCache.GetDynamicMethod(obj.GetType(), method, parameters); if (dynamicMethod == null) { ThrowHelper.ThrowNotImplementedException( ReflectionSR.MethodNotImplemented, obj.GetType().FullName, method, TypeHelper.GetTypesString(parameters)); } object result = null; try { result = dynamicMethod.Invoke(obj, parameters); } catch (Exception ex) { throw new CallMethodException( string.Format(ReflectionSR.Culture, ReflectionSR.MethodCallFailed, obj.GetType().FullName, method), ex); } return(result); }
public void Get() { DynamicMethodCache dynamicMethodCache = new DynamicMethodCache(); MethodInvoker methodInvoker = (o, parameters) => null; Func <MethodInvoker> creatorFunc = () => methodInvoker; MethodInvoker cachedMethodInvoker = dynamicMethodCache.GetOrAddDelegate( // null, new DynamicMethodInfo(), creatorFunc, DynamicMethodCacheStrategy.Temporary); creatorFunc = null; cachedMethodInvoker.ToStringInvariant(); }