VB.NET难以序列化带有附加事件的类(VB.NET difficulties serializing classes with attached events)

编程入门 行业动态 更新时间:2024-10-27 02:19:45
VB.NET难以序列化带有附加事件的类(VB.NET difficulties serializing classes with attached events)

惊讶这个问题还没有问到这里,但这里有:

在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.

更多推荐

本文发布于:2023-08-07 10:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   序列化   VB   NET   difficulties

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!