63246

In C#, how do I add event handlers to an object based on names?

I'm am trying to add event handlers to 'myObject' based on the name of an event or events. 'myObject' has an event called 'MyEvent' and I have a public event handler called 'MyEventHandler' (which I don't need to get by name but do it below to get a MethodInfo).

This is what I have so far:

EventInfo eventInfo = myObject.GetType().GetEvent("MyEvent"); MethodInfo handlerInfo = GetType().GetMethod("MyEventHandler"); if (eventInfo != null && handlerInfo != null) eventInfo.AddEventHandler( this, Delegate.CreateDelegate(eventInfo.EventHandlerType, handlerInfo) );

I get this error:

Error binding to target method.

Am I on the right track or is there a better way?

Answer1:

I had success with this code:

eventInfo.AddEventHandler(this, Delegate.CreateDelegate(typeof(eventInfo.EventHandlerType.FullName), this, "MyEventHandler"));

Edit: Added: eventInfoEventHandlerType.FullName so it's a little more dynamic based on the event handler type.

Recommend

  • startLeScan returns false
  • what is distributed queue?
  • How to show tree-view replies to message? Java & Hibernate
  • JBoss 7.1.1 does not create server.log
  • How to get the all the raw touch points at a particular instant for a tap?
  • How can I know which MenuItem or button was pressed
  • Implementing an Interface on a dynamic type with events
  • Tracking WP Post Tags using Google Analytics and dataLayer (GTM)
  • Blackberry add border to image when focus
  • RuntimeError: working outside of request context When request used in first line of method
  • Python Matplotlib FuncAnimation
  • Python create word plot between two lists in matplotlib to show list commonality
  • Error while uploading a new version of an Android app [duplicate]
  • Bar chart in seaborn
  • How to search duplicated key in KeyValuePair list and delete the earlier key value
  • Error creating glassfish as a service
  • Use a non-class member as an event handler
  • How to pass parameters to a method by reflection
  • how to import a .Jar file in Java Script
  • How to create c# console application to cosume the .net webservice [closed]
  • Symfony2 - Custom error pages are never shown
  • Get MethodInfo for any method with any signature (delegate for any signature)
  • video tag html5 with src from base64_encode not work on different device and browser
  • Get form values in Symfony2 controller , Child does not exist
  • Ninject: One interceptor instance per one class instance being intercepted?
  • Namespace error while declare it in global scope
  • Multithreaded Server receives data from just one client JAVA
  • Use Mockito-core for create mock of final class?
  • F# comparing discriminated unions' by case identifier
  • Android get Serial Number
  • Why String.ToString()? [duplicate]
  • BBC micro:bit - Radio string transfer random carriage returns
  • Mixing enums with event classes in an event aggregator
  • Task.IsCancelled doesn't work
  • First time running a ASP site, and getting server error in '/' Application
  • Blackberry 6: how to detect a long click on track pad?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • LevelDB C iterator
  • How can i traverse a binary tree from right to left in java?