using System.Linq.Expressions;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
public class DelegateData
public MethodInfo method;
public void DoStuff(string a, int b)
public static void Main()
DelegateData data = new DelegateData
method = typeof(A).GetMethod("DoStuff", BindingFlags.Instance | BindingFlags.Public),
args = new object[] { "hello", 3 },
ParameterExpression param0Expr = Expression.Parameter(data.target.GetType());
IEnumerable<Type> paramTypes = data.args.Select(arg => arg.GetType());
IEnumerable<ParameterExpression> methodParams = paramTypes.Select(paramType => Expression.Parameter(paramType)).ToList();
MethodCallExpression methodCallExpr = Expression.Call(param0Expr, data.method, methodParams);
IEnumerable<ParameterExpression> actionParams = new[] { param0Expr }.Union(methodParams);
Type[] actionParamTypes = new[] { data.target.GetType() }.Union(paramTypes).ToArray();
LambdaExpression lambdaExpr = Expression.Lambda
typeof(Action<,,>).MakeGenericType(actionParamTypes),
Delegate compiledDelegate = lambdaExpr.Compile();
IEnumerable<object> delegateArgs = new[] { new Closure(null, null), data.target }.Union(data.args).ToArray();
compiledDelegate.DynamicInvoke(delegateArgs);