C# 转换列表<;对象>;列出<;综合体>;其中complex是用户定义的类型
假设我有一门课C# 转换列表<;对象>;列出<;综合体>;其中complex是用户定义的类型,c#,list,casting,C#,List,Casting,假设我有一门课 public class Complex { } // this is what I mean when I say List<Complex> // its just a user-defined class or type if you will public class Complex{}//这就是我说List的意思 //如果您愿意,它只是一个用户定义的类或类型 现在让我们假设我有一个方法,它返回一个包含对象的列表 现在,假设我可以进一步保证列表中的每个对象
public class Complex { } // this is what I mean when I say List<Complex>
// its just a user-defined class or type if you will
public class Complex{}//这就是我说List的意思
//如果您愿意,它只是一个用户定义的类或类型
现在让我们假设我有一个方法,它返回一个包含对象的列表
现在,假设我可以进一步保证列表中的每个对象实际上都是Complex类型(换句话说,list//对不起,这太难读了!而且.Cast()也不起作用:(
return(ComplexCollection)((List)((List)complexElementsDictionary[“ComplexElementCollection”])。of type();
有关此代码段的一些详细信息:
ComplexCollection inherits List<Complex>
complexElementsDictionary is of type (Dictionary<string, List<object>)
ComplexCollection继承列表
complexElementsDictionary是一种类型(Dictionary要将列表转换为新类型,您只需执行以下简单操作
(ComplexCollection)(yourList.Select(x => (complexType)x).ToList());
这应该是可行的,基本上你可以在字典中查看对象列表,并将每个项转换为适当的类型,然后将其转换为列表,然后将列表转换为更具体的集合类型。我担心没有直接的解决方案。也许你可以使用ConvertAll:
// ol is of type List<Object>
List<Complex> cl = ol.ConvertAll(o=>(Complex)c);
//ol属于列表类型
列表cl=ol.ConvertAll(o=>(复合)c);
分解示例代码并稍微修改以使用Cast():
因为typedList根本不是ComplexCollection
的实例,并且编译器没有可用于从List
转换到ComplexCollection
的转换运算符
您需要创建实例并向其中添加复杂对象:
ComplexCollection collection = new ComplexCollection();
foreach(Complex c in typedList){
collection.Add(c);
}
或者仅用四行:
ComplexCollection collection = new ComplexCollection();
foreach(Complex c in complexElementsDictionary["ComplexElementCollection"].Cast<>(Complex)){
collection.Add(c);
}
ComplexCollection集合=新的ComplexCollection();
foreach(complexElementsDictionary[“ComplexElementCollection”]中的复数c。强制转换(复数)){
增加(c);
}
将列表
转换为列表
非常简单:
List<object> objectList = complexElementsDictionary["ComplexElementCollection"];
List<Complex> complexList = objectList.Cast<Complex>().ToList();
然后,带有return语句的示例变成:
return new ComplexCollection(
complexElementsDictionary["ComplexElementCollection"].Cast<Complex>()
);
返回新的ComplexCollection(
complexElementsDictionary[“ComplexElementCollection”].Cast()
);
如果.Cast不起作用,手动强制转换.Select()也不会起作用。@SamuelNeff,出于某种原因,我认为强制转换仅用于将IEnumerable转换为IEnumerable。实际上,我刚刚意识到它可以在一行中完成。如果使用IEnumerable类型的单个参数为ComplexCollection类创建构造函数(构造函数只需调用基类实现),然后可以编写:ComplexCollection collection=new ComplexCollection(complexElementsDictionary[“ComplexElementCollection”].Cast(复杂));你的回答也很有帮助,但你因为难以理解的评论而失分:p这对我来说最有意义的是它的写作方式和内容:)
ComplexCollection collection = new ComplexCollection();
foreach(Complex c in complexElementsDictionary["ComplexElementCollection"].Cast<>(Complex)){
collection.Add(c);
}
List<object> objectList = complexElementsDictionary["ComplexElementCollection"];
List<Complex> complexList = objectList.Cast<Complex>().ToList();
public ComplexCollection(IEnumerable<Complex> values) : base(values)
{
/* plus any other logic you have */
}
return new ComplexCollection(
complexElementsDictionary["ComplexElementCollection"].Cast<Complex>()
);