This question has been answered, but I would like to present you a cropped picture; and I think the rather trivial version is the "call of the general extension method", which can be used to reflect Count reflectively:
// get Enumerable (which holds the extension methods) Type enumerableT = typeof(Enumerable); // get the Count-method (there are only two, you can check the parameter-count as in above // to be certain. Here we know it the first, so I use the first: MemberInfo member = enumerableT.GetMember("Count")[0]; // create the generic method (instead of int, replace with typeof(yourtype) in your code) MethodInfo method = ((MethodInfo) member).MakeGenericMethod(typeof(int)); // invoke now becomes trivial int count = (int)method.Invoke(null, new object[] { yourcollection });
This works because you do not need to use the generic IEnumerable<> type to call Count , which is an Enumerable extension and accepts the IEnumerable<T> argument as the first parameter (this is an extension), but you do not need to specify this.
Please note that from reading your question, it seems to me that you should use generics for your types, which adds type safety to your project and allows you to use Count or something else. After all, the only thing that is certain is that everything is Enumerable , right? If so, who needs reflection?
source share