Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

Call an extension method on IEnumerable using reflection February 17, 2010

Filed under: C#,Coding — Rupert Bates @ 6:27 pm

I recently needed to invoke the Cast extension method on a List via reflection, and found it was a bit different to what I was used to
I tried:

var cast = typeof (List<string>).GetMethod("Cast");

but this returned null, then after a bit of digging around I realised that because Cast is an extension method, I needed to get it from the class it is defined in which is System.Linq.Enumerable. This code gets us the MethodInfo for Cast:

var cast = typeof (Enumerable).GetMethod("Cast");

Then because Cast is a generic method we need to provide it with the TResult type parameter as follows:

var constructedCast = cast.MakeGenericMethod(ListType);

Then we can invoke it:

var castVals = constructedCast.Invoke(null, new[]{ theList});

notice that the first parameter to Invoke is null because extension methods are static, and also that we pass in the List we want to invoke the method on, which then becomes the ‘this’ parameter of the extension method.

The result of Cast is a CastIterator and I wanted a List so I can then call ToList in the same way and I end up with a List cast to the type I am interested in.

//The result of Cast<TResult> is a a CastIterator so we need to call ToList on this
var toList = typeof (Enumerable).GetMethod("ToList");
var constructedToList = toList.MakeGenericMethod(ListType);
var castList = constructedToList.Invoke(null, new [] {castVals});
Advertisements
 

4 Responses to “Call an extension method on IEnumerable using reflection”

  1. ivowiblo Says:

    It’s well known that extension methods are “a nice way” to call static methods from a static class… in fact, when you create extension methods, you do it in a new static class. I don’t understand why you tried to get it from the List type…

    • Rupert Bates Says:

      I guess because it’s easy to forget that you’re calling an extension method. You’re right that as soon as you think about it it’s obvious that the method is defined somewhere else. The only issue then is working out which class it is actually defined in, which is not actually all that straightforward…

  2. Zack Says:

    I’m going to backup Rupert on this one. The reflection mechanisms are very powerful in .NET, but locating extension methods is currently a weak link. You need to search all available types to find the class that implements it. That’s not a cheap, simple, nor obvious operation.

  3. Sutikshan Says:

    Hi,
    I am not sure if Searching for Extension method in this particular example should be costly, if we provide type name to search into (as we r giving Enumerable here).
    But I am stuck in other problem here, it is throwing me following error:-
    “‘classname’ could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near simple identifier, line 1, column 1.”
    Though I am sure i have added required namespace. any clue?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s