代码之家  ›  专栏  ›  技术社区  ›  Greg

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

  •  1
  • Greg  · 技术社区  · 14 年前

    请参阅下面的代码,我在这里得到一个错误,只要是不可克隆的。

    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;
        }
    }
    
    2 回复  |  直到 14 年前
        1
  •  6
  •   SLaks    14 年前

    克隆动物是没有意义的 long .

    Dictionary<string, long> .

    new Dictionary<string, long>(otherDictionary) .

        2
  •  1
  •   Ilia G    14 年前
    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;
        }
    }