Possible Duplicates:
How to iterate the List in Reflection
Problem with IEnumerable in Reflection
Hi,
I am facing a problem while Iterating the List in reflection.
var item = property.GetValue(obj,null); // We dont know the type of obj as it is in Reflection.
foreach(var value in (item as IEnumerable))
{
//Do stuff
}
If i do this i will get the error like
Using the generic type ‘System.Collections.Generic.IEnumerable’ requires 1 type arguments
Please help me.
There’s a difference between the type
IEnumerableand the generic typeIEnumerable<T>. Currently it thinks you mean the generic one as you’ve included the namespaceSystem.Collections.Generic; The error message is complaining because you’ve not written theIEnumerable<T>generic type correctly.The non-generic
IEnumerabletype is declared in theSystem.Collectionsnamespace, so add a reference to it. (using System.Collections;).If you did mean to use the generic type then you should have something like this:
foreach(var value in (item as IEnumerable<string>))where string is the type of objectitemenumerates over.See IEnumerable and IEnumerable<T> as well as this information about generic types.