I’m trying to retrieve MethodInfo for Where method of Enumerable type:
typeof (Enumerable).GetMethod("Where", new Type[] {
typeof(IEnumerable<>),
typeof(Func<,>)
})
but get null. What am I doing wrong?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That previous answer works for some cases, however:
Action<IEnumerable<T>>. It will treat allAction<>as matches, for example,string.Concat(IEnumerable<string>)andstring.Concat<T>(IEnumerable<T>)will both match if searching for"Concat"with typeIEnumerable<>on the string type. What is really desirable is handling nested generic types recursively, while treating all generic parameters as matching each other regardless of name while NOT matching concrete types.type.GetMethod()does. So, you might get the method you wanted if you’re lucky, or you might not.BindingFlagsin order to avoid ambiguity, such as when a derived class method ‘hides’ a base class method. You normally want to find base class methods, but not in a specialized case where you know the method you’re looking for is in the derived class. Or, you might know you’re looking for a static vs instance method, public vs private, etc. and don’t want to match if it’s not exact.type.GetMethods(), in that it also doesn’t search base interfaces for methods when looking for a method on an interface type. OK, maybe that’s being picky, but it’s another major flaw inGetMethods()that has been a problem for me.type.GetMethods()is inefficient,type.GetMember(name, MemberTypes.Method, ...)will return only methods with a matching name instead of ALL methods in the type.GetGenericMethod()could be misleading, since you might be trying to find a non-generic method that happens to have a type parameter somewhere in a parameter type due to a generic declaring type.Here’s a version that addresses all of those things, and can be used as a general-purpose replacement for the flawed
GetMethod(). Note that two extension methods are provided, one with BindingFlags and one without (for convenience).Note that the
IsSimilarType(Type)extension method can be made public and might be useful on its own. I know, the name isn’t great – you’re welcome to come up with a better one, but it might get really long to explain what it does. Also, I added yet another improvement by checking for ‘ref’ and array types (refs are ignored for matching, but arrays dimensions must match).So, that’s how Microsoft should have done it. It’s really not that hard.
Yeah, I know, you can shorten some of that logic using Linq, but I’m not a huge fan of Linq in low-level routines like this, and also not unless the Linq is about as easy to follow as the original code, which is often NOT the case, IMO.
If you love Linq, and you must, you can replace the inner-most part of
IsSimilarType()with this (turns 8 lines into 1):One last thing: If you’re looking for a generic method with a generic parameter, such as
Method<T>(T, T[]), you’ll have to find a Type which is a generic parameter (IsGenericParameter == true) to pass in for the parameter type (any one will do, because of the ‘wildcard’ matching). However, you can’t just donew Type()– you have to find a real one (or build one with TypeBuilder). To make this easier, I added thepublic class Tdeclaration, and added logic toIsSimilarType()to check for it and match any generic parameter. If you need aT[], just useT.MakeArrayType(1).