我正在尝试使用Expressions(Microsoft.Scripting.Ast)并需要将委托变量与委托分配给另一个实例方法,然后调用该委托。不幸的是我很无能:(
var @delegate = Expression.Variable (typeof (Delegate));
var expression = Expression.Block(
new [] { @delegate },
Expression.Assign(@delegate, /* MISSED PART */),
Expression.Call(@delegate, typeof(Delegate).GetMethod("DynamicInvoke"))
);
请告诉我,如果我遗漏了什么。这是我最近开始实习的。所以它有可能完全没有意义^^
答案 0 :(得分:0)
答案是Expression.GetDelegateType(...)
以下是为MethodInfo
创建委托的代码段:
public static Type GetDelegateType (this MethodInfo methodInfo)
{
var parameterTypes = methodInfo.GetParameters ().Select (x => x.ParameterType);
var returnType = new[] { methodInfo.ReturnType };
var delegateTypes = parameterTypes.Concat (returnType).ToArray ();
return Expression.GetDelegateType (delegateTypes);
}