Search code examples
c#reflectiontypestypeofgetmethod

C# GetMethod info of Queryable class


I'm new at Reflection and I was trying the below peice of code

var queryableLastMethodInfo = typeof(Queryable).GetMethod("Last", new Type[]{ typeof(IQueryable<>) });

but queryableLastMethodInfo always returns null.

Could you please help?


Solution

  • This should give you the MethodInfo of the "Last" extension method that doesn't take a predicate:

    var queryableLastMethodInfo = typeof(Queryable).GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
        .FirstOrDefault(x => x.Name == "Last" && x.GetParameters().Count() == 1);
    

    ...and this should give you the other one:

    var queryableLastMethodInfo = typeof(Queryable).GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
        .FirstOrDefault(x => x.Name == "Last" && x.GetParameters().Count() == 2);