1

我可以用什么来代替可以克隆的“长”?

请参阅下面的代码,只要不可克隆,我就会在此处收到错误。

public static CloneableDictionary<string, long> returnValues = new CloneableDictionary<string, long>();

编辑:我忘了提到我想使用我找到的以下代码(见下文)。

public class CloneableDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TValue : ICloneable
{
    public IDictionary<TKey, TValue> Clone()
    {
        var clone = new CloneableDictionary<TKey, TValue>();

        foreach (KeyValuePair<TKey, TValue> pair in this)
        {
            clone.Add(pair.Key, (TValue)pair.Value.Clone());
        }
        return clone;
    }
}
4

2 回答 2

6

克隆一个long.

您应该使用常规的Dictionary<string, long>.

如果要克隆字典本身,可以编写new Dictionary<string, long>(otherDictionary).

于 2010-08-12T01:36:41.113 回答
1
public class CloneableDictionary<TKey, TValue> : Dictionary<TKey, TValue>
{
    public IDictionary<TKey, TValue> Clone()
    {
        var clone = new CloneableDictionary<TKey, TValue>();

        foreach (KeyValuePair<TKey, TValue> pair in this)
        {
            ICloneable clonableValue = pair.Value as ICloneable;
            if (clonableValue != null)
                clone.Add(pair.Key, (TValue)clonableValue.Clone());
            else
                clone.Add(pair.Key, pair.Value);
        }

        return clone;
    }
}
于 2010-08-12T01:52:58.477 回答