如何在c#中对Dictionary中的字典进行深层复制?

Dea*_*ock 2 c# winforms c#-4.0

我有四个字典,两个是(字典里面的字典),声明如下所示

    Dictionary<string, Dictionary<string, string>> dict_set = new Dictionary<string, Dictionary<string, string>>();
    Dictionary<string, Dictionary<string, string>> dict_Reset = new Dictionary<string, Dictionary<string, string>>();
    Dictionary<string, string> set_value = new Dictionary<string, string>();
    Dictionary<string, string> Reset_value = new Dictionary<string, string>();
Run Code Online (Sandbox Code Playgroud)

我想先在字典set_vlaue和Reset_value中添加元素.一旦添加了值,我就会将这些词典添加到其他两个词典中,如下所示.

dict_set.Add(condiName, set_value);
dict_Reset.Add(condiName, Reset_value);
 set_value.Clear();
 Reset_value.Clear();
Run Code Online (Sandbox Code Playgroud)

这些值正在添加,但是在添加set_value和reset_value词典之后,我想清除这两个词典set_value和reset_value,但是当清除set_value和reset_value时,dict_set和dict_reset中的数据也被清除了.

任何人都可以帮助我,在这种情况下如何创建词典的深层副本...

小智 6

我不知道你在工作流程中想要做什么,但为什么不重新进行清洁呢?

dict_set.Clear(); 
Run Code Online (Sandbox Code Playgroud)

至:

dict_set = new Dictionary<string, string>(); 
Run Code Online (Sandbox Code Playgroud)