无法投射通用类 [英] Unable to cast Generic Class

查看:66
本文介绍了无法投射通用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





界面IFurnitureDao {...} 

Class TableDao:IFurnitureDao {。 ..}
Class ChairDao:IFurnitureDao {...}
Class AlmirahDao:IFurnitureDao {...}





我有类型IFurnitureDao的通用类...



类MyGenericClass< T> 其中 T:IFurnitureDao {...} 





MyGenericClass的对象存储在ArrayList中,如下所示:



 ArrayList myList =  new  ArrayList { new  MyGenericClass< TableDao> {..}, new  MyGenericClass< ChairDao> {..} ,MyGenericClass< AlmirahDao> {..}} 



在从ArrayList中重新获取数据时,我尝试将teh对象转换为IFurnitureDao。



列表< MyGeneRicClass< IFurnitureDao>> newList = myList.OfType< MyGenericClass< IFurnitureDao>>()。toList(); 



我收到一条错误,指出

无法从MyGenericClass< TableDao>转换对象到MyGenericClass< IFurnitureDao>。 





任何人都可以指导我。谢谢!



问候,

Divya

解决方案

你不能将具体对象转换为接口。您的具体对象实现接口,但也可以实现其他接口或包含其他属性/方法。因此你不能倒退。



另外,界面不是一个对象。它只是一个对象的描述(或蓝图),它将应用于实现该接口的对象。



如果要将特定对象强制转换为一个通用对象,您将需要创建一个通用类,除了您的接口之外什么都不实现,然后在迭代具体对象列表时分配每个属性。或者,从您的接口创建一个基类,然后从基类派生您的具体对象。像这样:



接口IFurnitureDao {...} 

Class BaseDao:IFurnitureDao {... }
Class TableDao:BaseDao {...}
Class ChairDao:BaseDao {...}
Class AlmirahDao:BaseDao {...}




如果这样做,那么只要没有真正的差异就可以向后投射:

类MyGenericClass< ; T> 其中 T:BaseDao {...} < /   t  >  


您尝试的类不是通用的。您已经将泛型实例化为完整的类。



说, MyGenericClass<> 是通用的, MyGenericClass< TableDao> (如果 TableDao 是一个完整的类,而不是通用参数)不是。



如果它们是泛型类,那么整个铸造的想法都没有任何意义:没有泛型类的实例。

你的类是非泛型的,但是你无法转换它们,因为它们不是赋值兼容的。目前尚不清楚你为什么要分配它们的实例。



对于某些背景,请看: http://msdn.microsoft.com/en-us/library/ee207183.aspx [ ^ ]。



但是,到了解它,你应该更好地理解更基本的编程主题:类和实例,泛型类,完整类型,遗传实例化和其他通用主题。



< DD> -SA

Hi,

Interface IFurnitureDao{...}

Class TableDao : IFurnitureDao {...}
Class ChairDao : IFurnitureDao {...}
Class AlmirahDao : IFurnitureDao {...}



I have a generic class of type IFurnitureDao...

Class MyGenericClass<T> where T:IFurnitureDao {...}



The objects of MyGenericClass is stored in an ArrayList as shown below :

ArrayList myList = new ArrayList {new MyGenericClass<TableDao>{..} , new MyGenericClass<ChairDao>{..}, MyGenericClass<AlmirahDao>{..} }


While retriving the data from ArrayList I am tryign to cast teh objects into IFurnitureDao.

List<MyGeneRicClass<IFurnitureDao>> newList = myList.OfType<MyGenericClass<IFurnitureDao>>().toList();


I am receiving an error stating

unable to cast objects from MyGenericClass<TableDao> to MyGenericClass<IFurnitureDao>. 



Can anyone guide me on this . Thanks !

Regards,
Divya

解决方案

You can't cast concrete objects to interfaces. Your concrete objects implement the interface but may also implement other interfaces or contain additional properties/methods. Therefore you can't go backwards.

Plus, an interface is NOT an object. It is only a description (or blueprint) for an object which will be applied to an object that implements that interface.

If you want to cast your specific objects back to a generic object, you will need to create a generic class which implements nothing but your interface then assign each property as you iterate the list of concrete objects. Or, create a base class from your interface then derive your concrete objects from the base class. Like this:

Interface IFurnitureDao{...}

Class BaseDao : IFurnitureDao {...}
Class TableDao : BaseDao {...}
Class ChairDao : BaseDao {...}
Class AlmirahDao : BaseDao {...}



If you do this, then you can cast backwards as long as there are no real differences:

Class MyGenericClass<t> where T:BaseDao {...}</t>


The classes you are trying to case are not generic. You already instantiated generics into a complete classes.

Say, MyGenericClass<> is generic, MyGenericClass<TableDao> (if TableDao is a complete class, not generic parameter) is not.

If they were generic classes, the whole idea of casting would not make any sense: there are not instances of generic classes.
Your classes are non-generic, but you cannot cast them because they are not assignment-compatible. It's not clear why are you trying to assign instances of them.

For some background, please see this: http://msdn.microsoft.com/en-us/library/ee207183.aspx[^].

But, to understand it, you should get better understanding of more basic programming topics: classes and instances, generic classes, complete types, instantiation of genetics, and other generic topics.

—SA


这篇关于无法投射通用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆