- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的所有自定义实体集合都有一个基类,它的一个简单版本是这样的:
[Serializable]
public class CollectionBase<T> : List<T> where T : IEntity
{
public bool IsDirty {get;}
public new void Add(T item)
{
this.SetDirty();
base.Add(item);
item.MadeDirty += new EventHandler(item_MadeDirty);
}
// Other standard list methods overridden here
...
public void SetDirty() { } // Mark the collection as dirty
private void item_MadeDirty(object sender, EventArgs e)
{
this.SetDirty();
}
}
集合位于 Session
中的序列化类中(即 session 中的 Customer 类具有 Order 实体的集合)。问题是我的实体基类的 MadeDirty 事件如下:
[field: NonSerialized()]
public event EventHandler MadeDirty;
不幸的是,我不能只删除事件的 NonSerialized
属性,因为这会在部署到我的应用程序服务器时导致 session 状态服务器出现问题。
有什么方法可以捕获 CollectionBase 上的反序列化完成事件,以便我可以遍历所有项目并重新分配 session 中的每个反序列化的 MadeDirty
事件?即
private void OnDeserialized(object sender, EventArgs e)
{
foreach (T item in this)
{
item.MadeDirty+= new EventHandler(item_MadeDirty);
}
}
但是,我无法想象这是第一次有人遇到这个问题,那么还有更好的选择吗?
最佳答案
是的,您可以添加带有 special attribute 的方法以及您类(class)的正确签名:
[OnDeserializedAttribute()]
private void RunThisMethod(StreamingContext context)
{
// post-serialize your class
}
关于c# - 是否存在 OnDeserialized 事件或类似事件以便我可以确定某个类已被反序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2999085/
我使用一个可序列化的简单类。它有一个用于反序列化的构造函数: protected MyClass(SerializationInfo info, StreamingContext context) 和
我一直在阅读有关 .NET 中序列化的一些资料,并开始思考将 OnSerializing/OnSerialized/OnDeserializing/OnDeserialized 功能作为属性而不是接口
我有一个类维护对哈希表的引用并序列化/反序列化该哈希表。在调用 SerializationInfo.GetValue 之后,Hashtable 没有完全反序列化,因为反序列化发生在 IDeserial
[OnDeserialized] public static void OnDeserialized(this List self, StreamingContext context) { /
我的所有自定义实体集合都有一个基类,它的一个简单版本是这样的: [Serializable] public class CollectionBase : List where T : IEntity
除this post 中指出的以外我想在 XmlSerializer 开始反序列化之前采取行动(而不是在完成反序列化时) 背景:我有一个实现 INotifyPropertyChanged 的基类。此
我一直在尝试在类中的方法中实现 OnDeserialized 属性。在我使用 XmlSerializer 反序列化该类之后,该方法从未被调用。 即使序列化类是被序列化的主类的成员属性,也会调用 OnD
我正在尝试使用来自 this answer 的 JsonConverter给 Can I specify a path in an attribute to map a property in my
我有一个问题,我在三个小时的大部分时间里一直在努力解决这个问题。我几乎可以肯定我错过了一些非常明显的东西...... 我有一个简单的 XML 文件: 我有一个简
我曾尝试在我的 xml 中实现自动加密和解密,但它并不能正常工作,即数据未加密。可能是什么原因?我的代码如下所示。我正在使用 XmlSerializer 类。谢谢 [Serializable] pub
我们使用 JSON.NET 与 OnDeserialized 属性一起序列化数据,以便在反序列化后执行自定义代码: [OnDeserialized] internal void OnDeseriali
我们使用 JSON.NET 将我们的数据与 OnDeserialized 属性一起序列化以在反序列化后执行自定义代码: [OnDeserialized] internal void OnDeseria
所以我有一些这样的代码。 [DataContract] public class Example { SomeClass _someVar; [OnDeserializing]
是否有与 Java 中的 C# [OnSerialized] 和 [OnDeserialized] 属性等效的属性? 最佳答案 如果您使用默认的 Java 序列化,您可以覆盖用于序列化和反序列化的方法
我正在将现有的 .NET 类库改编为可移植类库。我正在使用配置文件 78(.NET 4.5、Windows Store 8、Windows Phone 8)而不是配置文件 158(它也针对 Silve
我是一名优秀的程序员,十分优秀!