void CreateGenerator(object service, Type delegateType, ParameterMapping parameterMapping) { var resultType = delegateType.FunctionReturnType(); var invoke = delegateType.GetMethod("Invoke"); var args = invoke.GetParameters().Select(x => x.ParameterType) .Append(resultType) .Append(delegateType) .ToList(); //Find a func method that matches all args var funcRegistration = DelegateActivators .FirstOrDefault(x => x.GetGenericArguments().Count() == args.Count()); if (funcRegistration != null) { var creator = funcRegistration.MakeGenericMethod(args.ToArray()); _generator = (a0, a1) => { return((Delegate)creator.Invoke(null, new[] { a0, service, parameterMapping.ResolveParameterMapping(delegateType) })); }; } }
private void CreateGenerator(object service, Type delegateType, ParameterMapping parameterMapping) { Type trailingItem = delegateType.FunctionReturnType(); MethodInfo method = delegateType.GetMethod("Invoke"); List <Type> args = (from x in method.GetParameters() select x.ParameterType).Append <Type>(trailingItem).Append <Type>(delegateType).ToList <Type>(); MethodInfo info2 = DelegateActivators.FirstOrDefault <MethodInfo>(x => x.GetGenericArguments().Count <Type>() == args.Count <Type>()); if (info2 != null) { MethodInfo creator = info2.MakeGenericMethod(args.ToArray()); this._generator = (a0, a1) => (Delegate)creator.Invoke(null, new object[] { a0, service, parameterMapping.ResolveParameterMapping(delegateType) }); } }