Add event handler dynamically having the delegate name in a string

后端 未结 1 1445
情书的邮戳
情书的邮戳 2020-12-06 23:40

I need to assign an eventHandler to an object, in this way:

_Element.AddHandler(UIElement.MouseLeftButtonUpEvent, 
    new RoutedEventHandler(Vars.Digit), tr         


        
相关标签:
1条回答
  • 2020-12-07 00:05

    Yes, you can find the method with Type.GetMethod, and the event with Type.GetEvent, then add a handler with EventInfo.AddEventHandler. It may be slightly fiddly - and there'll be a lot of places to put error handling - but it shouldn't be too hard. Don't forget to specify binding flags if you want to be able to bind non-public methods/events.

    0 讨论(0)
提交回复
热议问题