如何忽略事件类成员为二进制序列化? [英] How to ignore Event class member for binary serialization?

查看:264
本文介绍了如何忽略事件类成员为二进制序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要避免序列化的事件类成员,因为当事件是由未标记为可序列化的序列化将失败的对象处理。

我试图用在事件类成员的非序列化属性,但它无法编译。该行code:

 <非序列化(​​)>公共事件PropertyValueChanged()
 

产生以下错误:

  

属性NonSerializedAttribute   不能适用于   PropertyValueChanged',因为   属性是不是对这个有效   声明类型。

 公共事件PropertyValueChanged()编译但需要下面描述的额外处理
 

有另一种方式来避免事件序列会员?

这是不是一个问题,如果该事件被序列化之前没有处理,我可以解决它通过克隆的对象(忽略事件)。只是不知道是否有更好的方法。

感谢。

解决方案

在C#中,你可以做到这一点,如下,所以我的希望的这种转换为相同的VB。

请注意这仅适用于字段的事件(例如,你没有自己的添加 / 删除

  [字段:非序列化]
公共事件事件类型事件名称;
 

否则是这样的:

  [非序列化]
事件类型backingField;
公共事件事件类型{
    添加{backingField + =价值; }
    除去{backingField  -  =价值; }
}
 

I need to avoid serializing an Event class member because when the event is handled by an object that is not marked as Serializable the serialization will fail.

I tried using the NonSerialized attribute on the Event class member but it fails to compile. This line of code:

<NonSerialized()> Public Event PropertyValueChanged()

produces the following error:

Attribute 'NonSerializedAttribute' cannot be applied to 'PropertyValueChanged' because the attribute is not valid on this declaration type.

Public Event PropertyValueChanged() ' compiles but needs the extra handling described below

Is there another way to avoid serializing Event members?

This is not a problem if the event is not handled and I can work around it by cloning the objects (and ignoring the event) before serializing them. Just wondering if there is a better way.

Thanks.

解决方案

In C# you can do this as below, so I hope this translates identically to VB.

Note this only applies to field-like events (i.e. where you don't have your own add/remove):

[field: NonSerialized]
public event EventType EventName;

Otherwise something like:

[NonSerialized]
EventType backingField;
public event EventType {
    add { backingField += value; }
    remove { backingField -= value; }
}

这篇关于如何忽略事件类成员为二进制序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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