C# object to array

前端 未结 5 1540
失恋的感觉
失恋的感觉 2020-12-15 04:40

Using reflection I have an object which I need to cast into an iterable list of items (type unknown, will be object). Using the Watch window I can see my object is an array

5条回答
  •  既然无缘
    2020-12-15 04:56

    Try to cast to IEnumerable. This is the most basic interface all enumerables, arrays, lists etc. implement.

    IEnumerable myList = anArray as IEnumerable;
    if (myList != null)
    {
        foreach (object element in myList)
        {
            // ... do something
        }
    }
    else
    {
        // it's not an array, list, ...
    }
    

提交回复
热议问题