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 IEnumerable and the generic type IEnumerable<T>. Currently it thinks you mean the generic one as you've included the namespace System.Collections.Generic; The error message is complaining because you've not written the IEnumerable<T> generic type correctly.
The non-generic IEnumerable type is declared in the System.Collections namespace, 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 object item enumerates over.
See IEnumerable and IEnumerable<T> as well as this information about generic types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With