-
To get the
MethodInfoforEnumerable.OfType<T>()we can use:typeof(System.Linq.Enumerable).GetMethod("OfType", new Type[] { typeof(IEnumerable) }) -
For the
MethodInfoofEnumerable.Sum()we can use the similar:typeof(System.Linq.Enumerable).GetMethod("Sum", new Type[] { typeof(IEnumerable<int>) }) -
However, for the
MethodInfofor 'Enumerable.Reverse()` we have to make do with:typeof(Enumerable).GetMember("Reverse").OfType<MethodInfo>().First() -
When the method is overloaded, for example taking a predicate, such as
Enumerable.First()the getMethodInfocall becomes even more kludgy*:typeof(Enumerable).GetMember("First").OfType<MethodInfo>().Where(m => m.GetParameters().Length == 1).First()
All four methods appear extension methods define in Enumerable that extend either IEnumerable, IEnumerable<>, or specific specialisations of IEnumerable<> such as IEnumerable<int>.
Why do the calls typeof(Enumerable).GetMethod("Reverse", new Type { typeof(IEnumerable<>)}) and typeof(Enumerable).GetMethod("First", new Type[] { typeof(IEnumerable<>) }) return null and not a MethodInfo-object? What is the difference between the first two extension methods and the latter two?
* : See Get methodinfo for Enumerable.DefaultIfEmpty and http://ift.tt/1RnFki5 and Select Right Generic Method with Reflection
Aucun commentaire:
Enregistrer un commentaire