Accessing a Collection Through Reflection

前端 未结 9 2163
时光说笑
时光说笑 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:25

    If you're not using an instance of the object but rather a Type, you can use the following:

    // type is IEnumerable
    if (type.GetInterface("IEnumerable") != null)
    {
    }
    

提交回复
热议问题