Assuming I have an event subscription such as
_vm.PropertyChanged += OnViewModelAmountChanged;
How can I see that method name by reflection the way 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,
Berryl
A .Net event is simply a pair of methods named
add_Whateverandremove_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, andaddandremoveaccessors 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
GetInvocationListmethod of theDelegateclass (without reflection).For non-simple events, including all WinForms events, this approach will not work.