82

When I write code like this

[XmlIgnore]
[NonSerialized]
public List<string> paramFiles { get; set; }

I get the following error:

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


If I write

[field: NonSerialized]

I get the following warning

'field' is not a valid attribute location for this declaration.
Valid attribute locations for this declaration are 'property'.
All attributes in this block will be ignored.


If I write

[property: NonSerialized]

I get the following error (again):

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


How can I use [NonSerialized] on a property?

Marcus Mangelsdorf
  • 2,852
  • 1
  • 30
  • 40
IAdapter
  • 62,595
  • 73
  • 179
  • 242

5 Answers5

80

Simple use:

[XmlIgnore]
[ScriptIgnore]
public List<string> paramFiles { get; set; }

Hopefully, it helps.

Anton Norko
  • 2,166
  • 1
  • 15
  • 20
  • 2
    Indeed this even works in classic ASP.NET Web Service scenario by completely hiding the property name from the consumer (a great trick for those stuck in pre-WCF project) – timmi4sa Nov 20 '12 at 17:23
  • 2
    I did not need to use [ScriptIgnore] to make this work – Antonio Nicolaas Teyken Mar 06 '18 at 09:41
  • 1
    @AntonioNicolaasTeyken It depends on what you're using to serialize the data. In my case, [ScriptIgnore] prevented Json serialization. – General Grievance Jul 25 '19 at 16:03
  • One more note: `ScriptIgnoreAttribute` is available in the `System.Web.Script.Serialization` namespace ([MSDN](https://learn.microsoft.com/en-us/dotnet/api/system.web.script.serialization.scriptignoreattribute?redirectedfrom=MSDN&view=netframework-4.8)). – Mass Dot Net Sep 19 '19 at 15:42
  • Just to be clear, this works for binary serializer? Because the question already has XmlIgnore in the example so not sure how this can help. – Corey Alix Feb 27 '20 at 15:29
58

Well... the first error says that you can't do that... from http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx

 [AttributeUsageAttribute(AttributeTargets.Field, Inherited = false)]
 [ComVisibleAttribute(true)]
 public sealed class NonSerializedAttribute : Attribute

I suggest using backing field

 public List<string> paramFiles { get { return list;}  set { list = value; } }
 [NonSerialized]
 private List<string> list;
Matze
  • 5,100
  • 6
  • 46
  • 69
wiero
  • 2,176
  • 1
  • 19
  • 28
50

From C# 7.3 you may attach attributes to the backing field of auto-implemented properties.

Hence the following should work if you update your project's language to C# 7.3:

[field: NonSerialized]
public List<string> paramFiles { get; set; }
Bfed
  • 503
  • 4
  • 5
14

For those using JSON instead of XML you can use the [JsonIgnore] attribute on properties:

[JsonIgnore]
public List<string> paramFiles { get; set; }

Available in both Newtonsoft.Json and System.Text.Json (.NET Core 3.0).

Ziad Akiki
  • 2,601
  • 2
  • 26
  • 41
  • 1
    Please note, which package that is doing serialisation matters! If you're doing serialisation with Newtonsoft, don't use the `JsonIgnore` from System.Text.Json (or vice versa). – Andres Sepulveda Jun 14 '23 at 01:01
6

As of .NET 3.0, you can use DataContract instead of Serializable. With the DataContract though, you will need to either "opt-in" by marking the serializable fields with the DataMember attribute; or "opt-out" using the IgnoreDataMember.

The main difference between opt-in vs opt-out is that opt-out by default will only serialize public members, while opt-in will only serialize the marked members (regardless of protection level).

Tezra
  • 8,463
  • 3
  • 31
  • 68