Accessing a Collection Through Reflection

前端 未结 9 2142
时光说笑
时光说笑 2020-12-14 00:39

Is there a way to iterate (through foreach preferably) over a collection using reflection? I\'m iterating over the properties in an object using reflection, and when the pr

9条回答
  •  情歌与酒
    2020-12-14 01:18

    I had this issue, but instead of using reflection, i ended up just checking if it was IEnumerable. All collections implement that.

    if (item is IEnumerable)
    {
        foreach (object o in (item as IEnumerable))
        {
    
        }
    } else {
       // reflect over item
    }
    

提交回复
热议问题