11

I have an IEnumerable<T>, and I want to call the Enumerable.Contains method by reflection. I'm just struggling to get the syntax right. Here's what I currently have:

var containsMethod = typeof(Enumerable).GetMethod("Contains", 
  new[] {
    typeof(IEnumerable<T>), 
    typeof(T) 
  });

This just comes back with a null.

What is the correct way to get the MethodInfo?

1

2 Answers 2

19

What is the correct way to get the MethodInfo?

You have to find the generic method - which is unfortunately a bit of a pain - and then construct that with the appropriate arguments. In this case you know that there are only 2 Contains overloads, and the one you want has two arguments, so you can use:

var method = typeof(Enumerable).GetMethods()
                               .Where(m => m.Name == "Contains")
                               .Single(m => m.GetParameters().Length == 2)
                               .MakeGenericMethod(typeof(T));

You should then be able to invoke it appropriately.

Sign up to request clarification or add additional context in comments.

6 Comments

That's nice code just for this method and this class. In general, there must be more and more checks.
You may want to fold the check from the Where into Single, i.e. m => m.Name == "Contains" && m.GetParameters().Length == 2
@dasblinkenlight: I personally found it more readable in this form. You could fold the check in, sure. This is closer to how I think about it though - find all the Contains methods, then find the one which has the right number of parameters.
@nsinreal: Yes, in general it's a pain finding generic methods.
@JonSkeet: hm, I am understanding that there can be nice syntax for generic method choosing. Smth like GetMethods(types: (Type T) => new Type[] { typeof(IEnumerable<>).WithGenericTypes(T), T }) - through expressions. How do you think, is there real need of such syntax? Can it be writed more better?
|
-1

Inspired by the answer of How to get the correct MethodInfo for "Where" extension method.

((Func<IEnumerable<string>, string, bool>)Enumerable.Contains).Method.GetGenericMethodDefinition()

8 Comments

This doesn't answer the question. You already know the generic type parameter.
For me I just need to identify the method, so it can be used in other dynamic calling. Maybe the title is larger than what the answers want.
No, they want the MethodInfo for Enumerable.Contains with any generic type parameter.
GetGenericMethodDefinition will return the generic version. [Boolean Contains[TSource](System.Collections.Generic.IEnumerable1[TSource], TSource)]` This is what I get.
Then maybe you should make clear how to convert the output of this one line into a MethodInfo with one specified generic type parameter. The question is how to get Contains for a specified type so that it can be invoked for a typed collection.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.