How to iterate the List in Reflection

后端 未结 5 2029
粉色の甜心
粉色の甜心 2020-12-06 17:21

I am having one property called Students which is of type List.

In reflection i can get the value of Students Property.

Now the p

5条回答
  •  囚心锁ツ
    2020-12-06 17:54

    The way you tried is, is the right one. You just need to fix your code and cast the return value from GetValue:

    var collection = (List)studentPro.GetValue(studentObj,null);
    
    foreach(var item in collection)
    {
         if(item.StudentID == 33)
             //Do stuff
    }
    

提交回复
热议问题