I am trying to implement the following pattern function:
MethodInfo GetMethod(
Expression<Func<TTarget, EventHandler<TEventArgs>>> method)
I can provide an instance of TTarget if required
The desired usage is:
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)
{
}
Here's what I've tried so far:
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();
}
It prints out:
System.Delegate CreateDelegate(System.Type, System.Object, System.Reflection.Met
hodInfo)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…