クローン可能な「長い」の代わりに何を使用できますか?
-
27-09-2019 - |
質問
クローン可能な「長い」の代わりに何を使用できますか?
以下に、ここでエラーが発生しているコードを参照してください。
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;
}
}
解決
クローニングには意味がありません long
.
レギュラーを使用する必要があります Dictionary<string, long>
.
辞書自体をクローン化する場合は、書くことができます new Dictionary<string, long>(otherDictionary)
.
他のヒント
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;
}
}
所属していません StackOverflow