制作字典的深拷贝

Make a deep copy of a dictionary

我有一个名为 baseDictionary 的词典。键是一个字符串,值是一个名为 myData 的 class 的属性。具体来说,这些属性是:"age"(作为 int)、"nationality"(作为字符串)和 "income"(作为 double)。

所以 baseDictionary 有一些字符串作为键,每个键都有一系列与特定人相关的属性。 我想在某个时候制作这本词典的深层副本,这样我就可以在不修改原始词典内容的情况下使用这个新副本。 我在 Whosebug 中找到了一个答案,建议使用以下代码来执行此深拷贝:

public static Dictionary<TKey, TValue>
     CloneDictionaryCloningValues<TKey, TValue>(
         Dictionary<TKey, TValue> original) where TValue : ICloneable
{
    Dictionary<TKey, TValue> ret = new Dictionary<TKey, TValue>(
         original.Count, original.Comparer);

    foreach (KeyValuePair<TKey, TValue> entry in original)
    {
        ret.Add(entry.Key, (TValue) entry.Value.Clone());
    }
    return ret;
}

问题是我不明白我应该如何修改它以使其与我的词典一起使用。例如我试过:

public static Dictionary<string, myData> CloneDictionaryCloningValues<TKey, TValue>
                    (Dictionary<string, myData> original) where TValue : ICloneable
    {
        Dictionary<string, myData> ret = new Dictionary<string, myData>(original.Count,
                                                                original.Comparer);
        foreach (KeyValuePair<string, myData> entry in original)
        {
            ret.Add(entry.Key, (myData)entry.Value.Clone());
        }
        return ret;
    }

但是我收到以下错误并且不起作用。

Error 3 'Project2.myData does not contain a definition for 'Clone' and no extension method 'Clone' accepting a first argument of type 'Project2.myDatacould be found (are you missing a using directive or an assembly reference?)

我该如何解决这个问题?

您不应该修改 CloneDictionaryCloningValues() 例程,只需按原样使用即可。因为它是通用的,所以它可以与您的键(字符串)和值 class(mydata)一起使用。

但是:

要使此例程正常工作,您的 class 必须有一个 public Clone() 例程,即实现 ICloneable 接口。

如函数指定:您需要在类型 myData

上实现 ICloneable

另外:

  • 保留原始代码并简单地传递你的字典。
  • myData class 重命名为更具描述性的名称并以大写字符开头

您根本不需要更改 CloneDictionaryCloningValues 方法,如果您使 myData class 实现 ICloneable 接口:

public class myData : ICloneable {

  // your code

  public object Clone() {
    // whatever you need to create a copy, for example:
    return new myData() {
      age = this.age,
      nationality = this.nationality,
      income = this.income
    };
  }

}

你也可以rewrite/overload采用克隆的方法而不是要求IClonable接口:

public static Dictionary<TKey, TValue> CloneDictionaryCloningValues<TKey, TValue>
   (Dictionary<TKey, TValue> original, Func<TValue, TValue> clone)
{
  Dictionary<TKey, TValue> ret = new Dictionary<TKey, TValue>(original.Count, original.Comparer);
  foreach (KeyValuePair<TKey, TValue> entry in original) {
    ret.Add(entry.Key, clone(Value));
  }
  return ret;
}

然后使用创建项目副本的函数调用该方法:

myCopy = CloneDictionaryCloningValues(myOriginal, item => {
  // whatever you need to create a copy, for example:
  return new myData() {
    age = item.age,
    nationality = item.nationality,
    income = item.income
  };
});