简体   繁体   English

将字典添加到另一个字典C#

[英]Adding a dictionary to another dictionary C#

Is it possible to add a dictionary to another dictionary that contains different dictionary values? 是否有可能增加一个dictionary到另一个dictionary包含不同的字典中的值?

Dictionary<String, Func<String>> d1 = new Dictionary<String,Func<String>>();
Dictionary<String, Func<String,String>> d2 = new Dictionary<String, Func<String,String>>();
Dictionary<String, Dictionary> dHolder = new Dictionary<String, Dictionary>();
dHolder.add("key",d1);
dHolder.add("key",d2); 

You can do this if you use the IDictionary interface, which all dictionaries (no matter what type parameters they use) inherit from. 如果使用IDictionary接口,则可以执行此操作,所有字典(无论使用什么类型的参数)都从该接口继承。

Dictionary<String, Func<String>> d1 = new Dictionary<String,Func<String>>();
Dictionary<String, Func<String,String>> d2 = new Dictionary<String, Func<String,String>>();
Dictionary<String, IDictionary> dHolder = new Dictionary<String, IDictionary>();

dHolder.Add("key1", d1);
dHolder.Add("key2", d2); 

However, you'll need to go through the pain of unboxing the values when you retrieve them. 但是,当您检索值时,您将需要将其拆箱。

You can, but not quite the way you have written. 您可以但不完全是您编写的方式。

var dict1 = new Dictionary<string, ObjectTypeA>();
var dict2 = new Dictionary<string, ObjectTypeB>();
var holder = new Dictionary<string, IDictionary>();

holder["key1"] = dict1;
holder["key2"] = dict2;

The only issue you have is now you've lost your generic types, so you need to check what type of dictionary you have when you take the key out. 现在唯一的问题是您丢失了通用类型,因此在拔出钥匙时需要检查字典的类型。

var val = holder["key1"];    //Returns an IDictionary
var dictType1 = holder["key1"] as Dictionary<string, ObjectTypeA>;

//dictType1 will be null if it isn't a Dictionary<string, ObjectTypeA>

So you will have to do some type checking/conversion at some point, but you can still interact with it as an IDictionary if you don't need strong typing. 因此,您将不得不在某些时候做一些类型检查/转换,但是如果您不需要强类型输入,您仍然可以作为IDictionary与它进行交互。

holder["key1"]["newKey"] = new ObjectTypeA();    //Works
holder["key1"]["newKey2"] = new ObjectTypeB();   //Runtime exception

One way would be making TValue of dHolder an Object and then cast it to relevant type after retrieval. 一种方法是使dHolder的TValue成为Object ,然后在检索后将其转换为相关类型。

    Dictionary<String, Func<String>> d1 = new Dictionary<String, Func<String>>();
    Dictionary<String, Func<String, String>> d2 = new Dictionary<String, Func<String, String>>();
    Dictionary<String, Object> dHolder = new Dictionary<String, Object>();
    dHolder.Add("key1", d1);
    dHolder.Add("key2", d2);

    Dictionary<String, Func<String>> val1 = dHolder["key1"] as Dictionary<String, Func<String>>;
    Dictionary<String, Func<String, String>> val2 = dHolder["key2"] as Dictionary<String, Func<String, String>>;

Remarks: 备注:

  • as operator returns null if cast fails. as运算符失败,则as运算符返回null
  • You can also use TryGetValue method of the dictionary to safely retrieve value by its key. 您还可以使用字典的TryGetValue方法通过其键安全地获取值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM