Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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# 铸造IList<;[模型对象]>;到IList<;实体对象>;_C#_Asp.net Mvc 3 - Fatal编程技术网

C# 铸造IList<;[模型对象]>;到IList<;实体对象>;

C# 铸造IList<;[模型对象]>;到IList<;实体对象>;,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我可以在MVC 3.Net c#项目中将特定模型类的IList强制转换为类EntityObject的IList吗?特定的模型类(如果定义为EntityObject类的子类)。当我尝试强制转换时,我得到一个错误: 无法强制转换类型为的对象 “System.Collections.Generic.List1[GesDis.Models.tiposcursorsef]”到 类型 “System.Collections.Generic.IList1[System.Data.Objects.DataCla

我可以在MVC 3.Net c#项目中将特定模型类的IList强制转换为类EntityObject的IList吗?特定的模型类(如果定义为EntityObject类的子类)。当我尝试强制转换时,我得到一个错误:

无法强制转换类型为的对象 “System.Collections.Generic.List
1[GesDis.Models.tiposcursorsef]”到
类型
“System.Collections.Generic.IList
1[System.Data.Objects.DataClasses.EntityObject]”


不,如果你能投,你可以这样做:

class Base {}
class Derived : Base {}
class SomeOther : Base {}

var list = new List<Derived>();
var baseList = (IList<Base>)list;

baseList.Add(new SomeOther()); // oops, add SomeOther to a List<Derived>
类基类{}
派生类:基{}
类其他对象:基{}
var list=新列表();
var baseList=(IList)list;
baseList.Add(newsomeother());//哎呀,把其他东西添加到列表中

不,如果你能投下它,你可以这样做:

class Base {}
class Derived : Base {}
class SomeOther : Base {}

var list = new List<Derived>();
var baseList = (IList<Base>)list;

baseList.Add(new SomeOther()); // oops, add SomeOther to a List<Derived>
类基类{}
派生类:基{}
类其他对象:基{}
var list=新列表();
var baseList=(IList)list;
baseList.Add(newsomeother());//哎呀,把其他东西添加到列表中

不,如果你能投下它,你可以这样做:

class Base {}
class Derived : Base {}
class SomeOther : Base {}

var list = new List<Derived>();
var baseList = (IList<Base>)list;

baseList.Add(new SomeOther()); // oops, add SomeOther to a List<Derived>
类基类{}
派生类:基{}
类其他对象:基{}
var list=新列表();
var baseList=(IList)list;
baseList.Add(newsomeother());//哎呀,把其他东西添加到列表中

不,如果你能投下它,你可以这样做:

class Base {}
class Derived : Base {}
class SomeOther : Base {}

var list = new List<Derived>();
var baseList = (IList<Base>)list;

baseList.Add(new SomeOther()); // oops, add SomeOther to a List<Derived>
类基类{}
派生类:基{}
类其他对象:基{}
var list=新列表();
var baseList=(IList)list;
baseList.Add(newsomeother());//哎呀,把其他东西添加到列表中

这是一个协方差的情况。也就是说,仅仅因为
B
源自
a
,并且你可以分配
a=new B()
,这并不意味着你可以分配
t
t
。这只是两种不同的泛型,分配不起作用

相反,您需要这样做:

IList<EntityObject> entityObjects = modelObjects.Cast<EntityObject>().ToList();
IList entityObjects=modelObjects.Cast().ToList();

这是一个协方差的情况。也就是说,仅仅因为
B
源自
a
,并且你可以分配
a=new B()
,这并不意味着你可以分配
t
t
。这只是两种不同的泛型,分配不起作用

相反,您需要这样做:

IList<EntityObject> entityObjects = modelObjects.Cast<EntityObject>().ToList();
IList entityObjects=modelObjects.Cast().ToList();

这是一个协方差的情况。也就是说,仅仅因为
B
源自
a
,并且你可以分配
a=new B()
,这并不意味着你可以分配
t
t
。这只是两种不同的泛型,分配不起作用

相反,您需要这样做:

IList<EntityObject> entityObjects = modelObjects.Cast<EntityObject>().ToList();
IList entityObjects=modelObjects.Cast().ToList();

这是一个协方差的情况。也就是说,仅仅因为
B
源自
a
,并且你可以分配
a=new B()
,这并不意味着你可以分配
t
t
。这只是两种不同的泛型,分配不起作用

相反,您需要这样做:

IList<EntityObject> entityObjects = modelObjects.Cast<EntityObject>().ToList();
IList entityObjects=modelObjects.Cast().ToList();