Create a Non-Serialized Event in 2.0

G

Guest

I am having trouble creating an event that is non-serializable in 2.0. In
1.1, I would have just put the [field:NonSerialized] Attribute on the event
as this is the MS-endorsed hack for making it non-serializable.



What I'm trying to do is the following:

public delegate void Notification(object notificationObject);

public interface INotification

{

[field:NonSerialized]

event Notification NotificationEvent;

}



Like I said, this worked perfectly fine in 1.1, but in 2.0 I get the
following warning:

warning CS0657: 'field' is not a valid attribute location for this
declaration. Valid attribute locations for this declaration are 'method,
event'. All attributes in this block will be ignored.

If I change field to method or event, the Attribute barfs saying:

Attribute 'NonSerialized' is not valid on this declaration type. It is valid
on 'field' declarations only.


Are there other ways to do this with an EventHandler on an interface?
 
M

Mattias Sjögren

Are there other ways to do this with an EventHandler on an interface?

Why do you want to put it on the interface method? Interfaces don't
have any data to serialize anyway. Put the attribute on the event in
the implementing class instead.


Mattias
 
G

Guest

That is correct, interfaces do not have any data in them. All of the classes
behind the interface in this case should never have the event serialized.

I had thought that I tried that scenario, but did not.
Thanks.


Alternatively, I could have used add/remove accessors and stored the
eventhandler list myself, but I did not want to write that for all of my
events.

I have yet to find any documentation of this breaking change, or perhaps it
was a warning that was never caught in 1.1?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top