Get MethodInfo method for LINQ Any () method?

I am trying to create an expression that calls the LINQ Any () method call, and I cannot find the correct arguments to pass Type.GetMethod ().

In the docs, it looks like Any () is implemented as a member of the Enumerable class, and this seems to work because it shows methods called "Any":

var enumerableType = typeof (Enumerable);
var foo = enumerableType.GetMethods().Where(m => m.Name == "Any").ToList();

And when I applied the "Any" method, I get an AmbiguousMatchException.

Enumerable has two Any () methods, one accepting one IEnumerable parameter, and the other accepting IEnumerable and Func. I want the second, and theoretically all I need to do is pass an array containing two types:

var bar = enumerableType.GetMethod("Any", new[] { typeof(IEnumerable<>), typeof(Func<,>) });

But this always returns null.

What am I doing wrong?

+4
2
var foo = enumerableType.GetMethods(BindingFlags.Static | BindingFlags.Public)
            .First(m => m.Name == "Any" && m.GetParameters().Count() == 2);
+6

, , IEnumerable<> Func<,>, ( ):

var enumerableType = typeof(Enumerable);
var bar =
(
    from m in enumerableType.GetMethods(BindingFlags.Static | BindingFlags.Public)
    where m.Name == "Any"
    let p = m.GetParameters()
    where p.Length == 2
        && p[0].ParameterType.IsGenericType
        && p[0].ParameterType.GetGenericTypeDefinition() == typeof(IEnumerable<>)
        && p[1].ParameterType.IsGenericType
        && p[1].ParameterType.GetGenericTypeDefinition() == typeof(Func<,>)
    select m
).SingleOrDefault();
+3

All Articles