Fabrice's weblog

Tools and Source

News

My .NET Toolbox
An error occured. See the script errors signaled by your web browser.
No tools selected yet
.NET tools by SharpToolbox.com

Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET

transatlantys hot news

Contact

Me

Others

Selected content

Static method reflection

.NET reflection features are powerful and this is really what makes it a powerful platform. We don't use reflection everyday, but when we do it's really useful.
Don't you hate it though when you have to write code like this?:

MethodInfo method = typeof(MyClass).GetMethod("MyMethod");


The problem with this code is that we use a string to identify the method, which means that we don't get compile-time validation.
Ayende has an interesting approach to this problem. He gives you a solution that allows writing the following code instead:

MethodInfo method = GetMethod<int, string>(MyClass.MyMethod);

In this case, everything is strongly-typed and checked by the compiler.

Of course, nothing is perfect and this solution suffers from a number of limitations, but it's an interesting approach anyway. The limitations:
  • it works only with static methods,
  • the methods need to be accessible (public or in your code's reach),
  • we can't use a similar approach for properties.
Maybe one day we'll get support for this right from the compiler. Currently, there is typeof for types, but we are out of luck for methods, properties or parameters...

Update: Daniel Cazzulino has another option that is more complete.

Comments

Ayende Rahien said:

Just to comment, it works on instance methods as well.

# July 5, 2006 7:06 AM

Fabrice Marguerie said:

That's true, but it requires an object instance like this: GetMethod<int, string>(new MyClass().MyInstanceMethod), while you don't need an instance with typeof(MyClass).GetMethod("MyInstanceMethod");

# July 5, 2006 9:11 AM

Jason Haley said:

# July 5, 2006 9:46 AM

Fabrice Marguerie said:

Of course, this is not a problem if you call GetMethod from a method of the instance itself, as Ayende pointed out to me.

# July 5, 2006 5:55 PM

Fabrice's weblog said:

As a followup to my last post, you can read Daniel Cazzulino's post in which he presents a solution for

# July 6, 2006 11:36 AM

Daniel Cazzulino said:

Wow... that's cool... I can complement it with mine (which doesn't work on non-static members) and have the best of all worlds!

# July 6, 2006 1:10 PM

Fabrice Marguerie said:

Exactly, now we have support for static and instance methods, as well as for properties.

The next challenge is to find a solution for parameters so that we don't have to write the following, which contains a hard-coded string:

if (param1 == null)

 throw new ArgumentNullException("param1");

Eveything seems possible now with lambda expressions :)

# July 6, 2006 1:38 PM

Marc Brooks said:

You can do it now, without the lambda stuff, though it is nicer when you can use "var".  I've got a generic method invoker built that is strong typed (or weak typed if desired), works with static and instance methods and builds a LCG stub that does minor type coercision (if needed) and boxing/unboxing.  Check it out at http://musingmarc.blogspot.com/2006/07/power-of-generics-compels-you-power-of.html

# July 9, 2006 10:46 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)