Events and Reflection: Assigning Callback methods to events using Reflection.

Sometime back while coding for a project, i was faced with a unique problem. While let me explain what i was trying to do.

As all of you know, .NET provides us to define our own events which are executed on some condition. You can provide a function which will be executed when the event occurs, this function has to confirm to the delegate with which the event has been created. A little wierd way to put it i know, but there are whole lot of resources on the web which will throw a better light on the functionality of delegates and events, For now i am assuming you know what i mean :) (bear with me).

For e.g. (I want go into details) . You can check out MSDN for more details.

//Define a delegate which will take string as input parameter and returns true or false. this is part of the namespace
public delegate bool CheckHandler(string Value);

//Create a event which confirms to the delegate above, this is done inside the class
public event CheckHandler OnCheckHandler;

//define a callback which confirms to the delegate declared above
public bool OnCheck_Text(string SomeText){….}

//when you want to assign a callback method, you do it like this
OnCheckHandler += OnCheck_Text;

//call the call back method
if(OnCheckHandler  != null)
{
//This will call OnCheck_Text method
   OnCheckHandler  (“some value”);
}

So this is fair enough if you know the method at design time. Now lets come to my problem. How do you call a method which you dont know at design time? That is to say i want to assign new eventhandlers, without the recompiling the whole code. So what was natural for me at the time was to use reflection to access the callback method dynamically (may be there is another method, i  havent checked :) )

Away i went happily loading assemblies, creating types, invoking methods, then the bombshell, how do i assign a callback method to eventhandler?

Simply saying i cant do this

OnCheckHandler += _method; //method is a variable of type MethodInfo defined in System.Reflection namespace
//or this
OnCheckHandler += new CheckHandler(_method );

 

This doesnt work and the compiler will crib and spurt out some error messages. Too bad :( . After scouting on the net for solution, i didnt get much leeway, but yes there were some pointers here and there :)

So after lots of false start and some irritating moments, the solution shown itself, light at the end of the tunnel(perseverance pays).

This is how to go about it. The Delegate class has a static member called CreateDelegate, there are many overloaded function, but the one which would suffice our requirement is

public static Delegate CreateDelegate( Type type, MethodInfo method );

 

all you have to do is;

  • Create a variable of type delegate
  • Assign it the call back method(pointed by MethodInfo)
  • Cast the delegate variable to your delegate type
  • assign the delegate to your event handler
//OnCheckHandler += _method; //method is a variable of type MethodInfo defined in System.Reflection namespace

Delegate _delegate = Delegate.CreateDelegate(typeof(CheckHandler), _method);

CheckHandler _onCheck = _delegate as CheckHandler;
OnCheckHandler += _onCheck ;

 

Well thats all to it. Simple and straight forward >:) (Well if you know where to look for it)

Happy coding is all i can say :)

Leave a Reply