需要在 ComboBox 选择更改的事件处理程序中执行深层复制
DeepCopy 失败
但是完全相同的 DeepCopy 在 ctor 和按钮单击的事件处理程序中成功
已经在两台机器上重现了这个
愿意更改处理程序或深层复制但我在 ComboBox SelectionChanged 的事件处理程序中几乎需要它
在代码中查找 this 失败
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.ComponentModel;
namespace DeepCopy
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private List<TestClass> testClassList = new List<TestClass>();
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
try
{
TestClass tca = new TestClass();
tca.Prop1 = "Tcaa";
TestClass tcb = DeepClone<TestClass>(tca);
tca.Prop1 = "TcaNew1";
System.Diagnostics.Debug.WriteLine(tca.Prop1);
System.Diagnostics.Debug.WriteLine(tcb.Prop1);
tcb.Prop1 = "sdf1";
System.Diagnostics.Debug.WriteLine(tcb.Prop1);
}
catch (Exception Ex)
{
System.Diagnostics.Debug.WriteLine(Ex.Message);
}
TestClass tc = new TestClass();
tc.Prop1 = "tc1";
testClassList.Add(tc);
tc = new TestClass();
tc.Prop1 = "tc2";
testClassList.Add(tc);
foreach (TestClass tcx in TestClassList)
{
try
{
// this does not fail
TestClass tcb = DeepClone<TestClass>(tcx);
System.Diagnostics.Debug.WriteLine(tc.Prop1);
System.Diagnostics.Debug.WriteLine(tcb.Prop1);
tc.Prop1 += "ctorA";
System.Diagnostics.Debug.WriteLine(tc.Prop1);
System.Diagnostics.Debug.WriteLine(tcb.Prop1);
tcb.Prop1 += "ctorB";
System.Diagnostics.Debug.WriteLine(tcb.Prop1);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
public List<TestClass> TestClassList { get { return testClassList; } }
[Serializable()]
public abstract class TestAstract : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public abstract string Prop1 { get; set; }
}
[Serializable()]
public class TestClass : TestAstract
{
private string prop1;
public override string Prop1
{
get { return prop1; }
set
{
if (prop1 == value) return;
prop1 = value;
NotifyPropertyChanged("Prop1");
}
}
public TestClass() { }
}
public static T DeepClone<T>(T obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}
private void clickDeepCopy(object sender, RoutedEventArgs e)
{
foreach (TestClass tc in TestClassList)
{
try
{
// this does not fail
TestClass tcb = DeepClone<TestClass>(tc);
System.Diagnostics.Debug.WriteLine(tc.Prop1);
System.Diagnostics.Debug.WriteLine(tcb.Prop1);
tc.Prop1 += "clickA";
System.Diagnostics.Debug.WriteLine(tc.Prop1);
System.Diagnostics.Debug.WriteLine(tcb.Prop1);
tcb.Prop1 += "clickB";
System.Diagnostics.Debug.WriteLine(tcb.Prop1);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
private void cbSelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (TestClass tc in TestClassList)
{
try
{
// this fails
TestClass tcb = DeepClone<TestClass>(tc);
// A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
// Type 'System.ComponentModel.PropertyChangedEventManager' in Assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.
// A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
System.Diagnostics.Debug.WriteLine(tc.Prop1);
System.Diagnostics.Debug.WriteLine(tcb.Prop1);
tc.Prop1 += "cbceA";
System.Diagnostics.Debug.WriteLine(tc.Prop1);
System.Diagnostics.Debug.WriteLine(tcb.Prop1);
tcb.Prop1 = "cbceB";
System.Diagnostics.Debug.WriteLine(tcb.Prop1);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
}
}
<Window x:Class="DeepCopy.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="DeepCopy" Click="clickDeepCopy" />
<ComboBox Grid.Row="1" ItemsSource="{Binding Path=TestClassList}" DisplayMemberPath="Prop1" SelectionChanged="cbSelectionChanged" />
</Grid>
</Window>
更有趣
尝试了一个空的 ComboBox SelectionChanged 事件处理程序
调用空的 ComboBox SelectionChanged 后,按钮单击将失败
删除了 XAML 中的 ComboBox SelectionChanged 事件处理程序 在 ComboBox SelectionChanged
之后按钮单击将失败 - 即使没有事件处理程序
但按钮单击将在 ComboBox SelectionChanged 之前成功
删除了 XAML 中的 ComboBox SelectionChanged 事件处理程序 在 XAML 中
设置 SelectedIndex = 0
按钮单击事件处理程序将第一次失败
即使没有 SelectionChanged 事件处理程序并且 ComboBox 上没有 SelectedIndex,仍然会在 TestClassList 上调用 get
单独的原始绑定不会破坏深层复制
一旦在 ComboBox 上选择任何项目,深层复制就会中断
我知道你很可疑,但我在这上面花了几个小时