如何从[可序列化] INotifyPropertyChanged的实施者排除非序列化的观察员? [英] How to exclude nonserializable observers from a [Serializable] INotifyPropertyChanged implementor?
问题描述
我有近百项实体类的寻找这样的:
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));
}
}
注意 [字段:在
属性。这是必要的,因为一些观察员(在我的情况 - 显示实体版的网格)可能不是序列化和实体必须是可序列化,因为它提供了 - 通过远程处理 - 由分体的机器上运行的应用程序的PropertyChanged
非序列化]
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.
本解决方案工作正常简单的情形。但是,它可能是一些观察员是 [Serializable接口]
,并且需要被保留。我应该如何处理这种
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?
解决方案我正在考虑:
- 完全
ISerializable的
- 自定义序列化需要编写大量的代码,我不希望这样做 - 使用
[ OnSerializing]
和[OnDeserializing]
属性的PropertyChanged
手动序列化 - 但这些辅助方法提供仅SerializationContext
,其中AFAIK不会存储序列数据(的SerializationInfo
创建)
- 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)
推荐答案
您说得对,第一个选项是更多的工作。虽然它有可能给你一个更有效的实现,将您的实体有很多复杂化。想想看,如果你有一个基地实体
类,它实现 ISerializable的
,所有子类也可手动实现序列化的!
You're right that the first option is more work. While it can potentially give you a more efficient implementation, it will complicate your entities a lot. Consider that if you have a base Entity
class that implements ISerializable
, all subclasses also have to manually implement serialization!
诀窍获得第二个选项工作,是继续标记事件作为非序列,但有是的序列化,并且您在适当的序列化挂钩填充自己。下面是一个示例我刚写来告诉你如何:
The trick to getting the second option to work, is to continue marking the event as non-serializable, but to have a second field that is serializable and that you populate yourself during the appropriate serialization hooks. Here is a sample I just wrote to show you how:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var entity = new Entity();
entity.PropertyChanged += new SerializableHandler().PropertyChanged;
entity.PropertyChanged += new NonSerializableHandler().PropertyChanged;
Console.WriteLine("Before serialization:");
entity.Name = "Someone";
using (var memoryStream = new MemoryStream())
{
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, entity);
memoryStream.Position = 0;
entity = binaryFormatter.Deserialize(memoryStream) as Entity;
}
Console.WriteLine();
Console.WriteLine("After serialization:");
entity.Name = "Kent";
Console.WriteLine();
Console.WriteLine("Done - press any key");
Console.ReadKey();
}
[Serializable]
private class SerializableHandler
{
public void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Console.WriteLine(" Serializable handler called");
}
}
private class NonSerializableHandler
{
public void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Console.WriteLine(" Non-serializable handler called");
}
}
}
[Serializable]
public class Entity : INotifyPropertyChanged
{
private string _name;
private readonly List<Delegate> _serializableDelegates;
public Entity()
{
_serializableDelegates = new List<Delegate>();
}
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
OnPropertyChanged("Name");
}
}
}
[field:NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
protected void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
[OnSerializing]
public void OnSerializing(StreamingContext context)
{
_serializableDelegates.Clear();
var handler = PropertyChanged;
if (handler != null)
{
foreach (var invocation in handler.GetInvocationList())
{
if (invocation.Target.GetType().IsSerializable)
{
_serializableDelegates.Add(invocation);
}
}
}
}
[OnDeserialized]
public void OnDeserialized(StreamingContext context)
{
foreach (var invocation in _serializableDelegates)
{
PropertyChanged += (PropertyChangedEventHandler)invocation;
}
}
}
}
这篇关于如何从[可序列化] INotifyPropertyChanged的实施者排除非序列化的观察员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!