I have almost a hundred of entity classes looking like that:
[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));
}
}
Notice the [field:NonSerialized]
attribute on PropertyChanged
. This is necessary as some of the observers (in my case - a grid displaying the entities for edition) may not be serializable, and the entity has to be serializable, because it is provided - via remoting - by an application running on a separater machine.
This solution works fine for trivial cases. However, it is possible that some of the observers are [Serializable]
, and would need to be preserved. How should I handle this?
Solutions I am considering:
- full
ISerializable
- custom serialization requires writing a lot of code, I'd prefer not to do this - using
[OnSerializing]
and[OnDeserializing]
attributes to serializePropertyChanged
manually - but those helper methods provide onlySerializationContext
, which AFAIK does not store serialization data (SerializationInfo
does that)