7
我想實現以下模式功能:從表達式樹中提取方法名稱?
MethodInfo GetMethod(
Expression<Func<TTarget, EventHandler<TEventArgs>>> method)
如果需要,我可以提供t目標的一個實例
所需的用法是:
public static void Main(string[] args)
{
var methodInfo = GetMethod<Program, EventArgs>(t => t.Method);
Console.WriteLine("Hello, world!");
}
private void Method(object sender, EventArgs e)
{
}
這裏是我已經試過到目前爲止:
private static MethodInfo GetMethod(TTarget target, Expression<Func<TTarget, EventHandler<TEventArgs>>> method)
{
var lambda = method as LambdaExpression;
var body = lambda.Body as UnaryExpression;
var call = body.Operand as MethodCallExpression;
var mInfo = call.Method as MethodInfo;
Console.WriteLine(mInfo);
throw new NotImplementedException();
}
它打印出來:
System.Delegate CreateDelegate(System.Type, System.Object, System.Reflection.Met hodInfo)
使用您正在使用的編程語言標記問題可能會有幫助。它看起來像C#,但我不能確定。 – styfle