【发布时间】:2010-10-11 18:21:17
【问题描述】:
我有将近一百个看起来像这样的实体类:
[Serializable]
public class SampleEntity : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return this.name; }
set { this.name = value; FirePropertyChanged("Name"); }
}
[field:NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
private void FirePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
注意PropertyChanged 上的[field:NonSerialized] 属性。这是必要的,因为某些观察者(在我的情况下 - 显示要编辑的实体的网格)可能无法序列化,并且实体必须是可序列化的,因为它是通过远程处理 - 由运行在单独机器上的应用程序提供的.
此解决方案适用于琐碎的情况。但是,可能有一些观察者是[Serializable],需要保留。我该如何处理?
我正在考虑的解决方案:
- 完整的
ISerializable- 自定义序列化需要编写大量代码,我不想这样做 - 使用
[OnSerializing]和[OnDeserializing]属性手动序列化PropertyChanged- 但这些辅助方法仅提供SerializationContext,AFAIK 不存储序列化数据(SerializationInfo这样做)
【问题讨论】:
标签: c# serialization remoting inotifypropertychanged