Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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
.net 演员名单<;SomeClass>;列出<;对象>;_.net - Fatal编程技术网

.net 演员名单<;SomeClass>;列出<;对象>;

.net 演员名单<;SomeClass>;列出<;对象>;,.net,.net,我们可以将List转换为Listint.net吗 简单的演员阵容是做不到的。它们是两种不同的类型,并且这两种类型之间没有继承关系。但你可以这样做: foreach (Object item in MyList.Cast<object>() ) { //... } List<object> listOfObjects = people.ConvertAll(item => (object)item); foreach(MyList.Cast()中的对象项

我们可以将
List
转换为
List
int.net吗

简单的演员阵容是做不到的。它们是两种不同的类型,并且这两种类型之间没有继承关系。但你可以这样做:

foreach (Object item in MyList.Cast<object>() )
{
     //...
}
List<object> listOfObjects = people.ConvertAll(item => (object)item);
foreach(MyList.Cast()中的对象项)
{
//...
}

不,您可以通过这种方式强制转换数组,但不能强制转换列表

由于
SomeClass
始终可以强制转换为
object
,因此可以使用
cast
方法创建列表:

List<object> list = someClassList.Cast<object>().ToList();
List List=someClassList.Cast().ToList();

作为一个例子,这应该是可行的

List<int> intList=new List<int>();
intList.Add(1);
intList.Add(2);
List<object> listObject = intList.Cast<object>().ToList();
List intList=new List();
增加(1);
增加(2);
List listObject=intList.Cast().ToList();
关于:

List<object> listObj = myList.ConvertAll(item => (object)item);
列出:

List<Person> people = new List<Person>();
people.Add(new Person() { Name = "Al",  Score=97.3});
people.Add(new Person() { Name = "Bob",  Score = 99.2});
people.Add(new Person() { Name = "Charlie", Score=55.333});
List people=newlist();
添加(newperson(){Name=“Al”,Score=97.3});
添加(newperson(){Name=“Bob”,Score=99.2});
添加(newperson(){Name=“Charlie”,Score=55.333});
转换过程是这样的:

foreach (Object item in MyList.Cast<object>() )
{
     //...
}
List<object> listOfObjects = people.ConvertAll(item => (object)item);
listOfObjects=people.ConvertAll(item=>(object)item);

似乎工作得很好。但也许我遗漏了什么。显然,我在这里创建了一个新列表。想法?

嘿,太好了@贾扬塔:我相信这些是它的文档:如果你在foreach中指定object作为类型…@Massif-true,我认为你不需要.Cast。从子类型到父类型/接口的转换更有用,但我怀疑这就是这里真正发生的事情,对象只是问题的占位符。