[英]How to call method with EventHandler parameter using expression trees?
Consider this simple piece of code.考虑一下这段简单的代码。 How can this be done using expression trees?
如何使用表达式树来做到这一点?
ErrorsChangedEventManager.AddHandler(obj, obj.SomeHandler);
Here's a small sample illustrating what I'm trying to accomplish.这是一个小样本,说明了我要完成的工作。 (Add a reference to
WindowBase
to make it compile.) (添加对
WindowBase
的引用以使其编译。)
class Program : INotifyDataErrorInfo
{
public int Id { get; set; }
static void Main(string[] args)
{
var p1 = new Program { Id = 1 };
var p2 = new Program { Id = 2 };
// Here is the root of the problem.
// I need to do this INSIDE the expression from a given instance of Program.
EventHandler<DataErrorsChangedEventArgs> handler = p1.OnError;
var handlerConstant = Expression.Constant(handler);
var mi = typeof(ErrorsChangedEventManager).GetMethod(nameof(ErrorsChangedEventManager.AddHandler),
BindingFlags.Public | BindingFlags.Static);
var source = Expression.Parameter(typeof(INotifyDataErrorInfo), "source");
var program = Expression.Parameter(typeof(Program), "program");
// This will work, but the OnError method will be invoked on the wrong instance.
// So, I need to get the expression to perform what would otherwise be easy in code...
// E.g. AddHandler(someObject, p2.OnError);
var call = Expression.Call(mi, source, handlerConstant);
var expr = Expression.Lambda<Action<INotifyDataErrorInfo, Program>>(call, source, program);
var action = expr.Compile();
action.DynamicInvoke(p1, p2);
p1.ErrorsChanged.Invoke(p1, new DataErrorsChangedEventArgs("Foo"));
}
void OnError(object sender, DataErrorsChangedEventArgs e)
{
if (sender is Program p)
{
Console.WriteLine($"OnError called for Id={Id}. Expected Id=2");
}
}
public IEnumerable GetErrors(string propertyName) => Enumerable.Empty<string>();
public bool HasErrors => false;
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
}
Obviously, it doesn't work.显然,它不起作用。 I somehow need to provide the
OnError
handler as a parameter to the call.我不知何故需要提供
OnError
处理程序作为调用的参数。
It seems the easiest thing to do is to create a lambda which creates the EventHandler<DataErrorsChangedEventArgs>
for you, and then use Expression.Invoke
to call it:似乎最简单的做法是创建一个 lambda ,它为您创建
EventHandler<DataErrorsChangedEventArgs>
,然后使用Expression.Invoke
调用它:
public class Program : INotifyDataErrorInfo
{
public int Id { get; set; }
public static void Main()
{
var p1 = new Program { Id = 1 };
var p2 = new Program { Id = 2 };
var mi = typeof(ErrorsChangedEventManager).GetMethod(nameof(ErrorsChangedEventManager.AddHandler),
BindingFlags.Public | BindingFlags.Static);
var source = Expression.Parameter(typeof(INotifyDataErrorInfo), "source");
var program = Expression.Parameter(typeof(Program), "program");
Expression<Func<Program, EventHandler<DataErrorsChangedEventArgs>>> createDelegate = p => p.OnError;
var createDelegateInvoke = Expression.Invoke(createDelegate, program);
var call = Expression.Call(mi, source, createDelegateInvoke);
var expr = Expression.Lambda<Action<INotifyDataErrorInfo, Program>>(call, source, program);
var action = expr.Compile();
action(p1, p2);
p1.ErrorsChanged.Invoke(p1, new DataErrorsChangedEventArgs("Foo"));
}
public void OnError(object sender, DataErrorsChangedEventArgs e)
{
if (sender is Program p)
{
Console.WriteLine($"OnError called for Id={Id}. Expected Id=2");
}
}
public IEnumerable GetErrors(string propertyName) => Enumerable.Empty<string>();
public bool HasErrors => false;
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
}
If you look at the DebugView for createDelegate
, you can see that the compiler created:如果您查看
createDelegate
的 DebugView ,您可以看到编译器创建了:
.Lambda #Lambda1<System.Func`2[Program,System.EventHandler`1[System.ComponentModel.DataErrorsChangedEventArgs]]>(Program $p)
{
(System.EventHandler`1[System.ComponentModel.DataErrorsChangedEventArgs]).Call .Constant<System.Reflection.MethodInfo>(Void OnError(System.Object, System.ComponentModel.DataErrorsChangedEventArgs)).CreateDelegate(
.Constant<System.Type>(System.EventHandler`1[System.ComponentModel.DataErrorsChangedEventArgs]),
$p)
}
You could construct this expression yourself if you wanted, by getting the MethodInfo
for OnError
, then calling CreateDelegate
on it.如果需要,您可以自己构造此表达式,方法是获取
OnError
的MethodInfo
,然后在其上调用CreateDelegate
。
All of that said, you can just use a lambda to do all of that:综上所述,您只需使用 lambda 即可完成所有这些操作:
Expression<Action<INotifyDataErrorInfo, Program>> test = (source, program) =>
ErrorsChangedEventManager.AddHandler(source, program.OnError);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.