event syntax question

  • Thread starter Thread starter Pat Richey
  • Start date Start date
P

Pat Richey

i'm writing some events that need to be NonSerialized and to get it to
work i ended up doing something like:

[field: NonSerialized]
public event OnEventHandler myEvent;

because

[NonSerialized]
public event OnEventHandler myEvent;

didn't compile. my question is: what does the 'field:' do?

thanks,
pat
 
Pat,
didn't compile. my question is: what does the 'field:' do?
Causes the NonSerializedAttribute to be applied to the underlying field that
is created and not the Event itself. Remember that an event is implemented
in terms of a field & a couple of special methods.

Use ILDASM on the assembly to see specifics.

For info on Attributes & attribute targets see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_17.asp

Specifically:

http://msdn.microsoft.com/library/d...ry/en-us/csspec/html/vclrfcsharpspec_17_2.asp

Hope this helps
Jay

Pat Richey said:
i'm writing some events that need to be NonSerialized and to get it to
work i ended up doing something like:

[field: NonSerialized]
public event OnEventHandler myEvent;

because

[NonSerialized]
public event OnEventHandler myEvent;

didn't compile. my question is: what does the 'field:' do?

thanks,
pat
 
Pat Richey said:
i'm writing some events that need to be NonSerialized and to get it to
work i ended up doing something like:

[field: NonSerialized]
public event OnEventHandler myEvent;

because

[NonSerialized]
public event OnEventHandler myEvent;

didn't compile. my question is: what does the 'field:' do?

It's an attribute target specifier. From the C# spec:

<quote>
An attribute specified on an event declaration that omits event
accessors can apply to the event being declared, to the associated
field (if the event is not abstract), or to the associated add and
remove methods. In the absence of an attribute-target-specifier, the
attribute applies to the event declaration. The presence of the event
attribute-target-specifier indicates that the attribute applies to the
event; the presence of the field attribute-target-specifier indicates
that the attribute applies to the field; and the presence of the method
attribute-target-specifier indicates that the attribute applies to the
methods.
</quote>
 
Back
Top