private static ActionExecutor WrapVoidAction(VoidActionExecutor executor) { return(delegate(ControllerBase controller, object[] parameters) { executor(controller, parameters); return null; }); }
private static ActionExecutor WrapVoidAction(VoidActionExecutor executor) { return delegate(ControllerBase controller, object[] parameters) { executor(controller, parameters); return null; }; }
private static ConsumerMethodExecutor WrapVoidAction(VoidActionExecutor executor) { return(delegate(object target, object[] parameters) { executor(target, parameters); return null; }); }
private static ActionExecutor WrapVoidAction(VoidActionExecutor executor) { return(delegate(object[] parameters) { executor(parameters); return null; }); }
private static ActionExecutor VoidAction(VoidActionExecutor executor) { return(delegate(object target, object[] parameters) { executor(target, parameters); return null; }); }
private static ActionExecutorAsync WrapVoidActionAsync(VoidActionExecutor executor) { return(delegate(object target, object[] parameters) { executor(target, parameters); return Task.FromResult <object>(null); }); }
private static ActionExecutor GetExecutor(MethodInfo methodInfo) { Guard.ArgumentNotNull(methodInfo, "methodInfo"); // Parameters to executor ParameterExpression controllerParameter = Expression.Parameter(typeof(ControllerBase), "controller"); ParameterExpression parametersParameter = Expression.Parameter(typeof(object[]), "parameters"); // Build parameter list List <Expression> parameters = new List <Expression>(); ParameterInfo[] paramInfos = methodInfo.GetParameters(); for (int i = 0; i < paramInfos.Length; i++) { ParameterInfo paramInfo = paramInfos[i]; BinaryExpression valueObj = Expression.ArrayIndex(parametersParameter, Expression.Constant(i)); UnaryExpression valueCast = Expression.Convert(valueObj, paramInfo.ParameterType); // valueCast is "(Ti) parameters[i]" parameters.Add(valueCast); } // Call method UnaryExpression instanceCast = (!methodInfo.IsStatic) ? Expression.Convert(controllerParameter, methodInfo.ReflectedType) : null; MethodCallExpression methodCall = methodCall = Expression.Call(instanceCast, methodInfo, parameters); // methodCall is "((TController) controller) method((T0) parameters[0], (T1) parameters[1], ...)" // Create function if (methodCall.Type == typeof(void)) { Expression <VoidActionExecutor> lambda = Expression.Lambda <VoidActionExecutor>(methodCall, controllerParameter, parametersParameter); VoidActionExecutor voidExecutor = lambda.Compile(); return(WrapVoidAction(voidExecutor)); } else { // must coerce methodCall to match ActionExecutor signature UnaryExpression castMethodCall = Expression.Convert(methodCall, typeof(object)); Expression <ActionExecutor> lambda = Expression.Lambda <ActionExecutor>(castMethodCall, controllerParameter, parametersParameter); return(lambda.Compile()); } }
/// <summary> /// 初始化对象 /// </summary> /// <param name="methodInfo">方法对象</param> public ActionDispatcher(MethodInfo methodInfo) { this.IsVoid = methodInfo.ReturnType == typeof(void); this.ReturnType = methodInfo.ReturnType; this.Method = methodInfo; _parameters = TypeInvoke.GetParameter(methodInfo); if (this.IsVoid) { _executorVoid = GetExecutor <T>(methodInfo) as VoidActionExecutor <T>; } else { _executor = GetExecutor <T>(methodInfo) as ActionExecutor <T>; } //_executor = GetExecutor<T>(methodInfo); //MethodInfo = methodInfo; }
private static ActionExecutor WrapVoidAction(VoidActionExecutor executor) { return delegate(object[] parameters) { executor(parameters); return null; }; }
/// <summary> /// 创建不同的委托 /// </summary> /// <param name="methodInfo"></param> /// <returns>返回委托类型</returns> public static object GetExecutor(MethodInfo methodInfo) { // Parameters to executor ParameterExpression controllerParameter = Expression.Parameter(methodInfo.DeclaringType, "callclass"); ParameterExpression parametersParameter = Expression.Parameter(typeof(object[]), "parameters"); // Build parameter list if (!GetParameter(out List <Expression> parameters, parametersParameter, methodInfo.GetParameters())) { System.Diagnostics.Debug.WriteLine($"方法:{methodInfo},无法创建委托进行调用。"); return(null); } //List<Expression> parameters = new List<Expression>(); //ParameterInfo[] paramInfos = methodInfo.GetParameters(); //for (int i = 0; i < paramInfos.Length; i++) //{ // ParameterInfo paramInfo = paramInfos[i]; // BinaryExpression valueObj = Expression.ArrayIndex(parametersParameter, Expression.Constant(i)); // UnaryExpression valueCast; // if (paramInfo.ParameterType.IsByRef) // { // try // { // string fullName = $"{ paramInfo.ParameterType.FullName.Substring(0, paramInfo.ParameterType.FullName.Length - 1) }, {paramInfo.ParameterType.Assembly}"; // Type type = Type.GetType(fullName); // valueCast = Expression.Convert(valueObj, type); // } // catch (Exception) // { // System.Diagnostics.Debug.WriteLine($"方法:{methodInfo.ToString()},无法创建委托进行调用。"); // return null; // } // } // else // { // valueCast = Expression.Convert(valueObj, paramInfo.ParameterType); // } // // valueCast is "(Ti) parameters[i]" // parameters.Add(valueCast); //} // Call method UnaryExpression instanceCast = (!methodInfo.IsStatic) ? Expression.Convert(controllerParameter, methodInfo.ReflectedType) : null; MethodCallExpression methodCall = Expression.Call(instanceCast, methodInfo, parameters);// = methodCall // methodCall is "((TController) controller) method((T0) parameters[0], (T1) parameters[1], ...)" // Create function if (methodCall.Type == typeof(void)) { Expression <VoidActionExecutor <T> > lambda = Expression.Lambda <VoidActionExecutor <T> >(methodCall, controllerParameter, parametersParameter); VoidActionExecutor <T> voidExecutor = lambda.Compile(); //_executorVoid = voidExecutor; return(voidExecutor); //WrapVoidAction(voidExecutor); } else { // must coerce methodCall to match ActionExecutor signature UnaryExpression castMethodCall = Expression.Convert(methodCall, typeof(object)); Expression <ActionExecutor <T> > lambda = Expression.Lambda <ActionExecutor <T> >(castMethodCall, controllerParameter, parametersParameter); return(lambda.Compile()); } }