您现在的位置是:首页 > 电脑 > 

复制ObservableCollection项(Duplicate an ObservableCollection Item)

2025-07-17 13:21:32
复制ObservableCollection项(Duplicate an ObservableCollection Item) 我想在observablecollection中复制一个列表项。 当我做: TreasureCards[TreasureCards.Count - 1] = TreasureCards[CardPositio
复制ObservableCollection项(Duplicate an ObservableCollection Item)

我想在observablecollection中复制一个列表项。 当我做:

TreasureCards[TreasureCards.Count - 1] = TreasureCards[CardPosition];

它会创建特定列表项的副本,但随后会在我的UI中进行链接。 因此,如果我更改新的重复项目的名称,它将更改原始名称。 我知道我可以逐个完成每个属性(见下文),但有没有办法只复制整个项目?

TreasureCards[TreasureCards.Count - 1].ame = TreasurecCards[CardPosition].ame; TreasureCards[TreasureCards.Count - 1].Type= TreasurecCards[CardPosition].Type; // etc

I want to duplicate a list item in an observablecollection. When I do:

TreasureCards[TreasureCards.Count - 1] = TreasureCards[CardPosition];

It creates a copy of the specific list item but then they are linked in my UI. So if I change the new duplicated item's name, it changes the originals name. I know I could do each of the properties one by one (see below) but is there a way to just copy the entire item?

TreasureCards[TreasureCards.Count - 1].ame = TreasurecCards[CardPosition].ame; TreasureCards[TreasureCards.Count - 1].Type= TreasurecCards[CardPosition].Type; // etc

最满意答案

您没有复制该对象。 您正在创建对该对象的新引用。 仍然只有一个对象; 现在,在您的集合中有两个对它的引用,对象的任何更改都会被两个引用反映出来。

要创建新对象,可以在从Object派生的任何内容上调用MemberwiseClone() 。 此方法返回一个新实例,复制原始对象中所有字段的值。 所以你要这样做:

TreasureCards[TreasureCards.Count - 1] = TreasureCards[CardPosition].MemberwiseClone();

这种方法有两个局限性。 首先,它是一个浅拷贝,即原始对象中的任何参考字段都复制了它们的值。 因此,如果a.Foo是对Bar对象的引用,则a.MemberwiseClone().Foo将引用相同的Bar对象。 其次,该方法只是复制字段; 它不会调用新对象的构造函数。 根据班级的设计,这要么不重要,要么是真正的大交易。

通常,使类实现ICloneable并显式实现Clone()方法更安全,例如:

public TreasureCard Clone() { return new TreasureCard { ame = this.ame, Type = this.Type, ... }; }

You aren't duplicating the object. You're creating a new reference to the object. There's still only one object; now there are two references to it in your collection, and any change to the object is reflected by both references.

To create a new object, you can call MemberwiseClone() on anything that derives from Object. This method returns a new instance, copying the values from all fields in the original object. So you'd do:

TreasureCards[TreasureCards.Count - 1] = TreasureCards[CardPosition].MemberwiseClone();

There are two limitati with this method. First, it's a shallow copy, any reference fields in the original object have their values copied. So if a.Foo is a reference to a Bar object, a.MemberwiseClone().Foo will refer to the same Bar object. Second, the method just copies the fields; it doesn't call the new object's ctructor. Depending on the design of the class, this is either unimportant or a Really Big Deal.

Usually, it's safer to make the class implement ICloneable and explicitly implement a Clone() method, e.g.:

public TreasureCard Clone() { return new TreasureCard { ame = this.ame, Type = this.Type, ... }; }

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/654052.html

相关标签:无
上传时间: 2023-07-28 12:50:23
留言与评论(共有 5 条评论)
本站网友 至诚感通
18分钟前 发表
this is either unimportant or a Really Big Deal. Usually
本站网友 世友地板怎么样
5分钟前 发表
复制ObservableCollection项(Duplicate an ObservableCollection Item) 我想在observablecollection中复制一个列表项
本站网友 郑州门面房出租
24分钟前 发表
例如: public TreasureCard Clone() { return new TreasureCard { ame = this.ame
本站网友 无奈的爱
22分钟前 发表
即原始对象中的任何参考字段都复制了它们的值