C# 铸造IList<;[模型对象]>;到IList<;实体对象>;
我可以在MVC 3.Net c#项目中将特定模型类的IList强制转换为类EntityObject的IList吗?特定的模型类(如果定义为EntityObject类的子类)。当我尝试强制转换时,我得到一个错误: 无法强制转换类型为的对象 “System.Collections.Generic.ListC# 铸造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
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();