/// <summary> /// Uses reflection to dynamically invoke a method /// if that method is 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 CallMethodIfImplemented(object obj, string method, params object[] parameters) { DynamicMethodHandle cachedMethod = MethodCaller.GetCachedMethod(obj, method, parameters); if (cachedMethod == null || cachedMethod.DynamicMethod == null) { return(null); } return(MethodCaller.CallMethod(obj, cachedMethod, parameters)); }
/// <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="info"> /// MethodInfo for the method. /// </param> /// <param name="parameters"> /// Parameters to pass to method. /// </param> public static object CallMethod(object obj, MethodInfo info, params object[] parameters) { DynamicMethodHandle cachedMethod = MethodCaller.GetCachedMethod(obj, info, parameters); if (cachedMethod == null || cachedMethod.DynamicMethod == null) { throw new NotImplementedException(info.Name + " not implemented."); } return(MethodCaller.CallMethod(obj, cachedMethod, parameters)); }
private static DynamicMethodHandle GetCachedMethod(object obj, MethodInfo info, params object[] parameters) { MethodCacheKey key = new MethodCacheKey(obj.GetType().FullName, info.Name, MethodCaller.GetParameterTypes(parameters)); DynamicMethodHandle dynamicMethodHandle = null; if (MethodCaller._methodCache.TryGetValue(key, out dynamicMethodHandle)) { return(dynamicMethodHandle); } lock (MethodCaller._methodCache) { if (!MethodCaller._methodCache.TryGetValue(key, out dynamicMethodHandle)) { dynamicMethodHandle = new DynamicMethodHandle(info, parameters); MethodCaller._methodCache.Add(key, dynamicMethodHandle); } } return(dynamicMethodHandle); }
/// <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="methodHandle"> /// MethodHandle for the method. /// </param> /// <param name="parameters"> /// Parameters to pass to method. /// </param> private static object CallMethod(object obj, DynamicMethodHandle methodHandle, params object[] parameters) { object result = null; DynamicMemberMethod arg_08_0 = methodHandle.DynamicMethod; object[] array2; if (parameters == null) { object[] array = new object[1]; array2 = array; } else { array2 = parameters; } if (methodHandle.HasFinalArrayParam) { int methodParamsLength = methodHandle.MethodParamsLength; int num = array2.Length - (methodParamsLength - 1); object[] extrasArray = MethodCaller.GetExtrasArray(num, methodHandle.FinalArrayElementType); Array.Copy(array2, extrasArray, num); object[] array3 = new object[methodParamsLength]; for (int i = 0; i <= methodParamsLength - 2; i++) { array3[i] = parameters[i]; } array3[array3.Length - 1] = extrasArray; array2 = array3; } try { result = methodHandle.DynamicMethod(obj, array2); } catch (Exception ex) { throw new CallMethodException(methodHandle.MethodName + " method call failed.", ex); } return(result); }