I want to write a reuse function to raise an event through reflection.
After searching, I found this similar question: How do I create an event through reflection in .NET / C #?
It works until I register an event handler in the WinForm control and try to call it. The private field ' <EventName> ' just disappears.
Below is my simplified code that reproduces the problem:
Program.cs:
public static void Main() { Control control = new Control(); control.Click += new EventHandler(control_Click); MethodInfo eventInvoker = ReflectionHelper.GetEventInvoker(control, "Click"); eventInvoker.Invoke(control, new object[] {null, null}); } static void control_Click(object sender, EventArgs e) { Console.WriteLine("Clicked !!!!!!!!!!!"); }
Here is my ReflectionHelper class:
public static class ReflectionHelper {
Additional Information:
- An event in the same class: worked.
- An event in another class, a subclass in the same assembly: it works.
- An event in MY differs from build, debug, and release mode: it works.
- Event in WinForm, DevExpress, ...: does not work
Any help is appreciated.
reflection c # events
Thang tran
source share