I am having one property called Students which is of type List<Student>.
In reflection i can get the value of Students Property.
Now the problem is How to iterate the List of Students.
I need to check whether StudentID [ some value ] is in that collection.
var collection = studentPro.GetValue(studentObj,null);
//I need to iterate like this,
foreach(var item in collection)
{
if(item.StudentID == 33)
//Do stuff
}
Please help me.
You just need to cast it:
The value returned to you and stored in
varis of typeobject. So you need to cast it toList<Student>first, before trying looping through it.RANT
That is why I personally do not like
var, it hides the type – unless in VS you hover on it. If it was a declared with typeobjectit was immediately obvious that we cannot iterate through it.UPDATE
In order to do that, you can cast to
IEnumerable: