Introduction

A few days ago, I needed to dynamically invoke a method on a class. Saying only this, things appear very simple.As usual it got complicated... I figured out that the method was a generic one, and that the class had several overloads defined for the given method.

Background

When embracing this new task, I was obviously concerned with the performance output of the solution, and that's why I opted for the DynamicMethod class (new in .NET Framework 2.0) and Reflection Emit.There are several pages on the Web that compare the DynamicMethod approach with the Reflection Invoke one, with a very large performance boost for the first one (please read this MSDN magazine article).

To use the DynamicMethod class approach, one must be able to generate the IL code for the method. I have some knowledge on this subject, but you all can do it. Simply code your method in C# or another .NET language, compile it, and then use Lutz Roeder's .NET Reflector to look at the generated IL. With a little persistence you will get the job done.

The Solution

The development output is a simple static class with a few public methods that allow you to create a GenericInvoker for any generic method.

GenericInvoker is a delegate defined as follows.

Note: On methods that have no return value, a call to the delegate will always return null.

You can create an instance of the GenericInvoker delegate by simply calling one of the overrides for the GenericMethodInvokerMethod method on the DynamiMethods static class (included in the article source code archive).

Note: The GenericInvoker delegate creation can be a slow process. Therefore if you are going to use it in some kind of loop, you should consider caching the value to reduce the performance impact.

Using the Code

Here is a simple example on how to use the supplied DynamicMethods class:

Points of Interest

One of the major problems that I faced was related to the GetMethod method from the Type class.Although GetMethod works very well with normal type methods, it doesn't do so for generic methods. If the generic method has overloads, than the GetMethod call will always return null.

To overcome this limitation, I had to use the GetMethods method and iterate through all the type methods to get the correct one. Here is the code that gets the job done:

Lastly and the reason for this post, when the method for which I am trying to generate the delegate contains out parameters (probably the same with ref parameters), calling the delegate throws the following InvalidProgramException exception:

This is really good article,
I convert this code to VB but I've got a problem.
When I use DataRow, DataTable, an entity including DataRow or some other as a parameter, the invoker throws an InvalidCast Exception.
By the way, in VB , delegates can't take paramarray (in c# param) tag in the parameters, and I think the problem occurs for this.

I'm having some problems getting this to work with static classes. I've added overrides allowing the BindingFlags to be passed in so the code is successfully finding the correct static generic method on the class...

However, when I try and call the delegate... Boom! A first chance exception of type 'System.InvalidProgramException' occurred in Unknown Module.

Am I right in assuming that passing in a null instance for a static class should work or should I be doing something else?