VB.NET将序列化附加事件的类 [英] VB.NET difficulties serializing classes with attached events

查看:126
本文介绍了VB.NET将序列化附加事件的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

令人惊讶的是,这个问题在这里还没有被问到,但是这里是:

Surprised this question has not been asked yet here, but here goes:

VB.NET中的序列化有点痛苦。如果您使用标准的Serializable()属性,并尝试序列化具有连接到处理程序的事件的类,它将尝试序列化处理程序。

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.

来自C#背景我不习惯处理这个问题,最好的解决方案是使用CodeProject的一个类。

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.

在VB.NET中创建类的序列化是什么标准的方法?

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应用于事件的备份字段,使用属性上的字段:前缀。不幸的是,VB不支持字段:技巧,所以你需要使事件的备份字段显式,并应用NonSerializedAttribute。 Rocky Lhotka为此提供了示例代码。 a>

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.

这篇关于VB.NET将序列化附加事件的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆