问题
I have an object hierarchy:
MyObject
+Variable
+etc.
(So MyObject is the base class, and Variable is one of the subclasses).
I have the dictionaries for the specific types
private ConcurrentDictionary<string, Variable> variables
= new ConcurrentDictionary<string, Variable>();
etc.
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
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.
回答1:
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.
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
{
}
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);
来源:https://stackoverflow.com/questions/15108599/dictionary-compatibility-of-base-class-vs-subclass-values