Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with IEnumerable in Reflection [duplicate]

Tags:

c#

ienumerable

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.

like image 906
Thaadikkaaran Avatar asked Jan 29 '26 23:01

Thaadikkaaran


1 Answers

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.

like image 153
George Duckett Avatar answered Feb 01 '26 14:02

George Duckett