使用泛型类型创建字典。 [英] Creating dictionary with generic types.

查看:93
本文介绍了使用泛型类型创建字典。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试使用c#中的字典,如下所示



Hi ,

I''m trying to use a dictionary in c# as follows

Dictionary<Type,IStorage<IEntity>>

似乎是声明和初始化都没问题。

然后我创建一个

and seems to be the declaration and initialization are ok .
Then I create an instance of

Orderstore:IStorage<Order>

Orderstore实现IStorage和Order实现IEntity。



但是当我尝试将Orderstore添加到字典中,它说Argument type Orderstore不能分配给参数类型IStorage< order>

我想知道有谁可以帮我这个。在此先感谢。

where Orderstore implements the IStorage and Order implements IEntity.

But when I try to add Orderstore to the dictionary it says "Argument type Orderstore is not assignable to parameter type IStorage<order>"
I wonder anyone can help me with this. Thanks in advance.

推荐答案

使用下面给出的代码:



字典< type,isynchronizedstorage>< ; isynchronizableentity>> myOrders;



如果myOrders是字典的名称,那么使用



Orderstore< order> order = myOrders [index] as Orderstore;



TypeCast itas OrderSto0re,那么你的代码就可以了。



问候。
Use the code given below:

Dictionary<type,isynchronizedstorage><isynchronizableentity>> myOrders;

If myOrders is the name of the dictionary, then use

Orderstore<order> order = myOrders[index] as Orderstore;

TypeCast it "as OrderSto0re" then your code will work.

Regards.


这篇关于使用泛型类型创建字典。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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