views:

235

answers:

4

I can't find "field" listed as a C# keyword anywhere. Does anyone know the background on this?

A: 

The NonSerializedAttribute is only applicable to fields, you can use it as follows:

[NonSerialized]
public string myString;
Yuriy Faktorovich
and properties...
Stan R.
No just fields.
Yuriy Faktorovich
I stand corrected.
Stan R.
+1  A: 

This is necessary, for example, if you are marking an event as non-serializable. It specifies the target that the attribute applies to.

It is part of the attribute target syntax. From the specification:

attribute-target:
    field
    event
    method
    param
    property
    return
    type

See also the documentation for NonSerializedAttribute:

To apply the NonSerializedAttribute class to an event, set the attribute location to field, as shown in the following C# code.

[field:NonSerializedAttribute()]

public event ChangedEventHandler Changed;

Jason
Well you can't serialize an event, and the NonSerializedAttribute is only applicable to fields, so I don't understand what [field:NonSerializedAttribute()] does, I'm also surprised it compiles.
Yuriy Faktorovich
The point is that objects that subscribe to the event will be considered as part of the object graph and serialized when the object containing the event is serialized. To prevent them as being serialized you have to attribute the event with the `NonSerializedAttribute`. But as `NonSerializedAttribute` only applies to fields, you have to mark it with the attribute-target `field`.
Jason
Yuriy, I believe that delegates are compiled into classes, in which case when its serialized it they serialize with their invocation list. In this case you need to set it as [NonSerialized] however it throws a compile error, so you need to specify field:NonSerialized. I think they fixed it in .NET 3.5 however
Stan R.
@Jason I just used an XML Serializer and the event wasn't serialized.
Yuriy Faktorovich
I can see how the EventHandler class is marked as Serializable, how can I reproduce this behavior for an event?
Yuriy Faktorovich
@Yuriy, http://msdn.microsoft.com/en-us/magazine/cc163902.aspx#S1
Stan R.
Odd, "assembly" is missing. The important one.
Hans Passant
@nobugz: They are handled differently as part of the non-terminal `global-attributes`. Specifically, `global-attribute-target: assembly module`.
Jason
+1  A: 

This is meant to allow you to set NonSerialized attribute on fields, this is useful in serializing events.

For instance this would give you a compilation error

[NonSerialized]
public event SomeEventHandler SomeEvent;

To fix this you have to use field:

[field:NonSerialized]
public event SomeEventHandler SomeEvent;

More on this here -- Delegates and Serialization

Stan R.
But you're marking it as NonSerialized, and events can't be serialized.
Yuriy Faktorovich
Yuriy, I believe in .NET 3.0 events/delegates were serializable as classes, so you had to specify them as NonSerialized
Stan R.
Yuriy, please take a look at the following link http://msdn.microsoft.com/en-us/magazine/cc163902.aspx#S1
Stan R.
You are correct. I was using the XmlSerializer to serialize the class, which doesn't have event serialization. But the BinaryFormatter is fine with it.
Yuriy Faktorovich
+1  A: 

The C# compiler usually has no trouble figuring out what part of a declaration the attribute applies to. Field should never be an issue. I can think of two cases where you always have to use it:

  1. Attributes that apply to the assembly. Very visible in AssemblyInfo.cs
  2. An attribute applied to the return value of a P/Invoke declaration, [return:MarshalAs]
Hans Passant