基类的字典兼容性与子类值 [英] Dictionary compatibility of base class vs. subclass values

查看:138
本文介绍了基类的字典兼容性与子类值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象的层次结构:

I have an object hierarchy:

MyObject
+Variable
+etc.

(因此为MyObject是基类和变量是子类之一)。

(So MyObject is the base class, and Variable is one of the subclasses).

我的字典为特定类型

private ConcurrentDictionary<string, Variable> variables 
    = new ConcurrentDictionary<string, Variable>();

等。

和我愿把所有的各种列表在一个较高水平的字典,在那里我会找到所有的具体名单:

and I would like to put all the various lists in one high level dictionary, where I would find all the specific lists:

private Dictionary<Type, ConcurrentDictionary<string, MyObject>> objects 
    = new Dictionary<Type, ConcurrentDictionary<string, MyObject>>();

但我不能具体的词典添加到高水平词典:

But I cannot add the specific dictionary to the high level dictionary:

objects.Add(typeof(Variable), variables); // DOES NOT COMPILE

有没有做我想做的一种手段?我不想定义变量列表,

Is there a means to do a what I want? I would not want to define the variables list as

private ConcurrentDictionary<string, MyObject> variables 
    = new ConcurrentDictionary<string, MyObject>(); // WORKS, BUT NOT NICE TO USE

所以,我想使用的具体名单,通过了对象的字典来执行特定类型的操作,也让所有的对象类型通用操作,这样我就不需要code一切手动为每个子类型。

So I want to use the specific lists to perform type specific operations, but also enable generic operations on all object types via the 'objects' dictionary, so that I don't need to code everything manually for each sub type.

例如,我想定义一个方法是这样的:

For example, I would like to define a method like this:

    public List<Variable> GetVariables()
    {
        return variables.Values.ToList();
    }

所以,我可以使用可变对象时,我知道他们都是变量。

So I could use Variable objects when I know that they are all variables.

推荐答案

正如你可以在你的例子看,你正试图完成什么是不可能的,因为你正在尝试添加类型 ConcurrentDictionary&LT对象;字符串变量&GT; 和类型的对象 ConcurrentDictionary&LT;字符串,为MyObject&GT; 来具有很强的类型作为第二个参数的字典 ConcurrentDictionary&LT;字符串,为MyObject&GT; 。因此,你不能添加的第一个类型的对象。这将是,当你想添加像字符串作为第二个参数字典&LT; INT,字符串&GT; 对象,它是不可能的。

As you can see in your example, what you are trying to accomplish is not possible since you are trying to add object of type ConcurrentDictionary<string, Variable> and object of type ConcurrentDictionary<string, MyObject> to the dictionary which has strong type as the second argument of ConcurrentDictionary<string, MyObject>. Therefore you can't add the objects of the first type. It would be like when you want to add double and string as the second argument to the Dictionary<int, string> object which is not possible.

但有一种解决方法可能是可行的。如果所有类型( MyObject的,可变等)在同一界面中获得比你可以做这样的事情:

But there is a workaround which might be doable. If all your types (MyObject, Variable etc.) derive from the same Interface than you could do something like this:

public interface MyInterface
{          
}

public class MyObject:MyInterface
{
}

public class Variable:MyInterface
{
}

比你可以称之为code,类似于一个你想要的。当您检索字典你愿意,你可以投你的对象的具体类型或使用通用的方法和多态性将达到其目的。

Than you could call this code, similar to one you wanted. When you retrieve the dictionary you want you could cast your objects to the specific type or use common methods and polymorphism will serve its purpose.

ConcurrentDictionary<string, MyInterface> myClass1Dict = new ConcurrentDictionary<string, MyInterface>();
myClass1Dict.TryAdd("one", new MyObject());
myClass1Dict.TryAdd("two", new MyObject());
ConcurrentDictionary<string, MyInterface> myClass2Dict = new ConcurrentDictionary<string, MyInterface>();
myClass1Dict.TryAdd("one", new Variable());
myClass1Dict.TryAdd("two", new Variable());
ConcurrentDictionary<Type, ConcurrentDictionary<string, MyInterface>> objectDict = new ConcurrentDictionary<Type, ConcurrentDictionary<string, MyInterface>>();
objectDict.TryAdd(Type.GetType(MyObject), myClass1Dict);
objectDict.TryAdd(Type.GetType(Variable), myClass2Dict);

这篇关于基类的字典兼容性与子类值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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