wpf IEditableObject 实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14997504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IEditableObject implementations
提问by Tommy Jakobsen
I've been looking to implement a generic class that implements IEditableObject for some of my ViewModels to inherit in order to support cancellations of editings on the properties in that ViewModel. The primary use of this is for modal dialogs in WPF that has OK and Cancel buttons.
我一直在寻求实现一个泛型类,该类实现 IEditableObject 以便我的一些 ViewModel 继承以支持取消对该 ViewModel 中的属性的编辑。它的主要用途是用于 WPF 中具有 OK 和 Cancel 按钮的模式对话框。
I've found the following implemetations online:
我在网上找到了以下实现:
- Generic implementation of IEditableObject via TypeDescriptor and Reflection
- Paul Stovell: IEditableObject Adapter for WPF and Windows Forms
- IEditableObject 通过 TypeDescriptor 和 Reflection 的通用实现
- Paul Stovell:用于 WPF 和 Windows 窗体的 IEditableObject 适配器
This seems overly complicated to me, but I'm unsure if I'm missing some of the functionality they provide. For one they don't support deep copy, so in my world we could just use MemberwiseClone to perform the shallow copy. Something like:
这对我来说似乎过于复杂,但我不确定我是否缺少它们提供的某些功能。其中一个他们不支持深拷贝,所以在我的世界中,我们可以只使用 MemberwiseClone 来执行浅拷贝。就像是:
private Item backupCopy;
private bool inEdit;
public void BeginEdit()
{
if (inEdit) return;
inEdit = true;
backupCopy = this.MemberwiseClone() as Item;
}
public void CancelEdit()
{
if (!inEdit) return;
inEdit = false;
this.Name = backupCopy.Name;
}
public void EndEdit()
{
if (!inEdit) return;
inEdit = false;
backupCopy = null;
}
This example should of course be in a generic abstract base class for the ViewModels to inherit, but you get the idea...
这个例子当然应该在一个通用的抽象基类中供 ViewModels 继承,但你明白了......
What is the difference here? What are the disadvantages of my approach besides not supporting deep copy? How would you modify it to support deep copy (not really sure if it's necessary yet)?
这里有什么区别?除了不支持深拷贝之外,我的方法还有什么缺点?您将如何修改它以支持深度复制(还不确定是否有必要)?
Update:
更新:
Found this article showing a better implementation than mine using reflection. Still much simpler than the other two articles I linked to:
发现这篇文章显示了比我使用反射更好的实现。仍然比我链接的其他两篇文章简单得多:
How can we extend this to support deep copy?
我们如何扩展它以支持深度复制?
采纳答案by daryal
In fact, MemberwiseCloneapplies a shallow copy. If you apply a shallow copy, reference type objects inside your object (like some reference type properties) are not created, just references are copied. So each instance will refer to the same objects.
实际上,MemberwiseClone应用的是浅拷贝。如果应用浅拷贝,则不会创建对象内的引用类型对象(如某些引用类型属性),只会复制引用。所以每个实例将引用相同的对象。
In deep copy, reference type objects inside the object are also copied.
在深拷贝中,对象内部的引用类型对象也被拷贝。
Think of a list of items; if you apply shallow copy, you will end up with two lists pointing to the same objects. If you apply deep copy, new objects will be created for the new list.
想想项目清单;如果你应用浅拷贝,你最终会得到两个指向相同对象的列表。如果应用深复制,将为新列表创建新对象。
I advise you to go with deep copy, instead of a shallow copy. Think about the list example; if you apply a shallow copy and change any element in the copied list, then it will not be possible to revert all changes back; since copied list shares the same elements with the original list.
我建议你使用深拷贝,而不是浅拷贝。想想列表的例子;如果您应用浅拷贝并更改复制列表中的任何元素,则将无法恢复所有更改;因为复制的列表与原始列表共享相同的元素。

