惊讶这个问题还没有问到这里,但这里有:
在VB.NET中进行序列化有点痛苦。 如果使用标准的Serializable()属性,并尝试序列化一个具有附加到处理程序事件的类,它也会尝试序列化处理程序。
来自C#背景我不习惯处理这个问题,我能找到的最好的解决方案是使用CodeProject的类。
不过,我宁愿不改变这个想法的对象继承结构。
在VB.NET中创建类的序列化的标准方法是什么?
我听说过删除所有处理程序,序列化,然后重新添加所有处理程序,但不能导致事件触发和不处理?
Surprised this question has not been asked yet here, but here goes:
Serialization in VB.NET is a bit of a pain. If you use the standard Serializable() attribute, and attempt to serialize a class that has events which are attached to handlers, it will attempt to serialize the handlers as well.
Coming from a C# background I am not used to dealing with this problem, and the best solution I can find is using a class off of CodeProject.
However I'd rather not change the object inheritance structure for this idea.
What is the standard way to do Serialization of classes created in VB.NET?
I have heard of removing all handlers, serializing, then re-adding all handlers, but couldn't that lead to events firing and not getting handled?
最满意答案
在C#中,解决方案是将NonSerializedAttribute应用于事件的后台字段,使用属性上的field:前缀。 不幸的是,VB不支持该field:技巧,所以你需要使事件的支持字段显式化,并将NonSerializedAttribute应用于该字段。 Rocky Lhotka为此提供了示例代码。
不幸的是,它看起来很冗长乏味,因为现在你必须说明每个事件的添加,删除和提升方法。
In C#, the solution is to apply the NonSerializedAttribute to the event's backing field, using the field: prefix on the attribute. Unfortunately, VB doesn't support the field: trick, so you need to make the event's backing field explicit, and apply NonSerializedAttribute to that. Rocky Lhotka has sample code for this.
Unfortunately it does look rather verbose and tedious, since now you have to spell out the add, remove and raise methods for each event.
更多推荐
发布评论