I have a very simple class:
class Trace { void WriteTrace() { Console.WriteLine("Trace !"); } }
I want this class to subscribe to an event, such as a form control load event. Management and event are defined dynamically, so I want to use reflection for this. I am trying something like this:
In my Trace class, I have this method:
public void SubscribeEvent (Control control) { if (type.IsAssignableFrom(control.GetType())) { Trace test = this; MethodInfo method = typeof(Trace).GetMethod("WriteTrace");
There is an error in the last line: Error binding to the target method. What is wrong in my fragment?
Thanks!
EDIT : Ok, there’s no more error, but when the “Download” event is added from the form, the WriteTrace method is not called (I set a breakpoint, but it isn’t reached), why?
Sorry for editing, it works very well :)
Florian
source share