Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need "field:" in my attribute declaration "[field:NonSerialized]"?

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

like image 387
skb Avatar asked Jan 07 '10 16:01

skb


People also ask

What is NonSerialized in C#?

The NonSerialized attribute marks a variable to not be serialized.

How do you make a field not serializable in C#?

When using the BinaryFormatter or SoapFormatter classes to serialize an object, use the NonSerializedAttribute attribute to prevent a field from being serialized. For example, you can use this attribute to prevent the serialization of sensitive data.


1 Answers

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;

like image 126
jason Avatar answered Oct 24 '22 11:10

jason