如何在不知道 C# 中的底层类型的情况下遍历数组

假设我有一组对象,其中一些是数组。


我想连接所有值来打印它们,但我不知道数组中元素的类型。


foreach(var item in myList)

{

    var val = item.Property;

    if (val.GetType().IsArray)

    {

        var array = val as IEnumerable<object>;

        val = string.Join(",", array);

    }

    DoSomething(val);

}

如果 val 包含 a string[],则此代码片段将起作用,也适用于 a myClass[]。


但是如果它包含一个int[]or double[],则数组将为空,这意味着动态转换失败。


如果 int 或 double 是从 System.TypeValue 继承的,而 System.TypeValue 是从 object 继承的,为什么此代码片段不起作用?


我怎么能做到这一点?


编辑:将代码片段更改为更明确,并避免出现错误的变量使用,因为我错误地简化了我的代码。


慕田峪9158850
浏览 188回答 3
3回答

白板的微信

在 C# 中是不允许这样做的,更多细节在这里。但是您可以转换为non-generic&nbsp;IEnumerable,然后在推送到之前将所有内容转换为 object&nbsp;string.Join():foreach(var val in myList){&nbsp; &nbsp; if (val.GetType().IsArray)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var array = (IEnumerable)val;&nbsp; &nbsp; &nbsp; &nbsp; // It's not allowed to set val variable, but I assume it's just an example&nbsp; &nbsp; &nbsp; &nbsp; val = string.Join(",", array.Cast<object>());&nbsp; &nbsp; }}

温温酱

首先,尝试分配到循环val内部是foreach行不通的。您无法更改正在迭代的集合。所以你需要建立一个新的集合。像这样的工作,看看yield return迭代器中的语句如何让您构建一个新IEnumerable的叶对象,并适用于任何级别的对象和值类型。&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var myWackyList = new object[] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new[]{1d, 2d},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3d,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new[]{4d, 5d},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new[]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6d&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "7",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new[]{ "8", "9"}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine( string.Join(", ", Flatten( myWackyList )));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; static IEnumerable<object> Flatten(IEnumerable enumerable)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var val in enumerable)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( val.GetType().IsArray )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ( var flattenedVal in Flatten( val as IEnumerable ) )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return flattenedVal;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return val;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP