如何对只读 OrderedDictionary 进行深度复制,其中键和值是不再只读的字符串?

发布于 2024-11-19 05:04:00 字数 228 浏览 2 评论 0原文

orderedDictionary 实例化是这样的:

IOrderedDictionary orderedDictionary= gridview.DataKeys[index].Values;

orderedDictionary 是只读的。

如何制作非只读的orderedDictionary 深层副本?序列化/反序列化不起作用,因为它还会复制只读部分。

The orderedDictionary instantiation is this:

IOrderedDictionary orderedDictionary= gridview.DataKeys[index].Values;

orderedDictionary is read only.

How can I make a deep copy of orderedDictionary that is not read only? Serialization/deserialization doesn't work cause it also copies the read only part.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇见了你 2024-11-26 05:04:00

最简单的方法是复制对象:

var newDictionary = new OrderedDictionary();
foreach(DictionaryEntry de in orderedDictionary)
{
    newDictionary.Add(de.Key, de.Value);
}

更新:
此代码不会创建字典中值的深层副本。
示例:

var orderedDictionary = new OrderedDictionary();
orderedDictionary.Add("1", new List<int> { 1, 2 });

var newDictionary = new OrderedDictionary();
foreach(DictionaryEntry de in orderedDictionary)
{
    newDictionary.Add(de.Key, de.Value);
}

两个字典都将包含一个带有键“1”的条目和相同的列表。从任何字典中的此列表中删除项目也会更改另一字典中的列表内容,因为只有一个列表。

Console.WriteLine(((List<int>)orderedDictionary["1"]).Count);
Console.WriteLine(((List<int>)newDictionary["1"]).Count);
Console.WriteLine(ReferenceEquals(orderedDictionary["1"], newDictionary["1"]));
((List<int>)orderedDictionary["1"]).Remove(1);
Console.WriteLine(((List<int>)orderedDictionary["1"]).Count);
Console.WriteLine(((List<int>)newDictionary["1"]).Count);

这将输出以下内容:

2
2
True
1
1

将新值分配给一个字典中的键,但对另一个字典没有影响:

newDictionary["1"] = new List<int>{3,4};
Console.WriteLine(ReferenceEquals(orderedDictionary["1"], newDictionary["1"]));
Console.WriteLine(((List<int>)orderedDictionary["1"]).Count);
Console.WriteLine(((List<int>)newDictionary["1"]).Count);

这将输出:

False
2
3

The easiest way would be to just copy the objects:

var newDictionary = new OrderedDictionary();
foreach(DictionaryEntry de in orderedDictionary)
{
    newDictionary.Add(de.Key, de.Value);
}

UPDATE:
This code will NOT create a deep copy of the values in the dictionary.
Example:

var orderedDictionary = new OrderedDictionary();
orderedDictionary.Add("1", new List<int> { 1, 2 });

var newDictionary = new OrderedDictionary();
foreach(DictionaryEntry de in orderedDictionary)
{
    newDictionary.Add(de.Key, de.Value);
}

Both dictionary will contain one entry with the key "1" and the same list. Removing an item from this list in any of the dictionaries will also change the contents of the list in the other dictionary, because there only IS one list.

Console.WriteLine(((List<int>)orderedDictionary["1"]).Count);
Console.WriteLine(((List<int>)newDictionary["1"]).Count);
Console.WriteLine(ReferenceEquals(orderedDictionary["1"], newDictionary["1"]));
((List<int>)orderedDictionary["1"]).Remove(1);
Console.WriteLine(((List<int>)orderedDictionary["1"]).Count);
Console.WriteLine(((List<int>)newDictionary["1"]).Count);

This will output the following:

2
2
True
1
1

Assigning a new value to a key in one of the dictionary however has no effect on the other dictionary:

newDictionary["1"] = new List<int>{3,4};
Console.WriteLine(ReferenceEquals(orderedDictionary["1"], newDictionary["1"]));
Console.WriteLine(((List<int>)orderedDictionary["1"]).Count);
Console.WriteLine(((List<int>)newDictionary["1"]).Count);

This will output:

False
2
3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文