Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从对象访问数组(任何元素类型)的元素_C#_Arrays_Ienumerable_Enumerable - Fatal编程技术网

C# 从对象访问数组(任何元素类型)的元素

C# 从对象访问数组(任何元素类型)的元素,c#,arrays,ienumerable,enumerable,C#,Arrays,Ienumerable,Enumerable,我使用管理来访问设备属性,并在下面编写了一段代码来创建字典数组。我的应用程序在列表视图控件中显示属性;所以我需要将所有属性值转换为简单字符串 Dictionary[]getInfo(字符串k){ //使用'k'作为管理密钥 var mos=新的ManagementObjectSearcher($“从{k}中选择*); var devices=新列表(); var mosc=mos.Get();//mosc是具有相同密钥的所有设备的集合 foreach(mosc中的var装置){ var prop

我使用管理来访问设备属性,并在下面编写了一段代码来创建字典数组。我的应用程序在
列表视图
控件中显示属性;所以我需要将所有属性值转换为简单字符串

Dictionary[]getInfo(字符串k){
//使用'k'作为管理密钥
var mos=新的ManagementObjectSearcher($“从{k}中选择*);
var devices=新列表();
var mosc=mos.Get();//mosc是具有相同密钥的所有设备的集合
foreach(mosc中的var装置){
var properties=newdictionary();
foreach(device.Properties中的var p){
如果(p.Value!=null){
如果(p.IsArray){
//我这里有问题
//我的应用程序必须将p.value转换为字符串
变量集合=(IEnumerable)p.Value
properties[p.Name]=string.Join(“,”,collection.Select(x=>x.ToString());
}否则
properties[p.Name]=p.Value.ToString();
}else属性[p.Name]=“”;
}
设备。添加(属性);
}
返回设备。ToArray();
}
p.Value
类型是
object
,但有时它包含一个数组,如
UInt[]
String[]
,我从中找到了部分代码,但它对我没有帮助,它说:

System.InvalidCastException:“无法将类型为”System.UInt16[]”的对象强制转换为类型为“System.Collections.Generic.IEnumerable`1[System.object]”

我也尝试了下面的代码,但它说的是相同的事情:

int[]数组=新的int[]{0,1,2};// 
IEnumerable
T
中是协变的,因此允许:

IEnumerable<Giraffes> giraffes = ....
var animals = (IEnumerable<Animal>)giraffes;
组成对象的位不会改变,只有引用的类型会改变,因此转换的名称也会改变。请注意,在第一个示例中,
动物
长颈鹿
是同一个对象,
对象。ReferenceEquals(动物,长颈鹿)
将返回
true
;我们只更改了引用它的变量的类型

在您的情况下,要从
IEnumerable
中获取
IEnumerable
,您必须枚举并装箱每个项,以创建所需类型的新枚举。为此,可以使用扩展方法
Enumerable.Cast()

IEnumerable objects=array.Cast();
或者自己做投影:

IEnumerable<object> objects = array.Select(i => (object)i);
IEnumerable objects=array.Select(i=>(object)i);
第二个代码:

int[]数组=新的int[]{0,1,2};
objectobj=数组;
var obj_array=(array)obj;
IEnumerable collection=obj_array.Cast();
字符串输出=string.join(“,”,collection.Select(x=>x.ToString());
因此,主要代码是:

Dictionary[]getInfo(字符串k){
//使用'k'作为管理密钥
var mos=新的ManagementObjectSearcher($“从{k}中选择*);
var devices=新列表();
var mosc=mos.Get();//mosc是具有相同密钥的所有设备的集合
foreach(mosc中的var装置){
var properties=newdictionary();
foreach(device.Properties中的var p){
如果(p.Value!=null){
如果(p.IsArray){
var数组=(数组)p.Value;
var collection=array.Cast();
properties[p.Name]=string.Join(“,”,collection.Select(x=>x.ToString());
}否则
properties[p.Name]=p.Value.ToString();
}else属性[p.Name]=“”;
}
设备。添加(属性);
}
返回设备。ToArray();
}

谢谢您的回答,但第二个代码不是我的主要问题。我的主要问题是第一个代码,在那里我无法访问
array
我只能在
object
类型中使用
p.Value
var array = new[] { 1, 2, 3 };
var objects = (IEnumerable<object>)array; //will not compile
var animal = (Animal)giraffe;
var o = (object)"Hello";
IFish fish = cod;
//etc.
IEnumerable<object> objects = array.Cast<object>();
IEnumerable<object> objects = array.Select(i => (object)i);