finding the Delegate Method registered to an Event
Assuming I have an event subscription such as
_vm.PropertyChanged += OnViewModelAmountChanged;
How can I see that method name by reflection the way开发者_如何学Go you can see it in the debugger? I got as far as the line below before I decided I might be in the Twilight Zone
?vm.GetType().GetEvent("PropertyChanged").EventHandlerType.GetMethod("GetInvocationList")
Can someone get me back to Earth? Can this also be done via expressions?
Cheers,
BerrylA .Net event is simply a pair of methods named add_Whatever
and remove_Whatever
. They are not guaranteed to be backed by field.
When you write event EventHandler Whatever;
in C#, it will automatically generate a private field with the same name as the event, and add
and remove
accessors that set the field.
You can inspect these at runtime by using Reflection to get the value of the private field, then callings the public GetInvocationList
method of the Delegate
class (without reflection).
For non-simple events, including all WinForms events, this approach will not work.
One thing to remember is that an event may customize the add/remove methods for an event. In this case, the containing class could put the delegate into any data structure (a List, e.g.) or even ignore it (though this would be unlikely). The important point is that the delegate could be stored any way the class likes. It would be like trying to find the field that is used to store a Property's value.
If you are within the class that declares the event, you can do something like that :
foreach(Delegate d in MyEvent.GetInvocationList())
{
Console.WriteLine(d.Method.Name);
}
精彩评论