NonSerialized on property

101,123

Solution 1

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;

Solution 2

Simple use:

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

Hopefully, it helps.

Solution 3

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; }

Solution 4

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).

Solution 5

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).

Share:
101,123
IAdapter
Author by

IAdapter

Updated on October 29, 2020

Comments

  • IAdapter
    IAdapter over 3 years

    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?

  • Ramon Zarazua B.
    Ramon Zarazua B. over 11 years
    Using the suggested backing field does not seem to work. The property is still being serialized.
  • wiero
    wiero over 11 years
    Yes it works, but not in every class named AaaSerializer. Which serializer are you using?
  • timmi4sa
    timmi4sa over 11 years
    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)
  • Setyo N
    Setyo N almost 11 years
    The correct attribute is [NonSerialized], not [NonSerializable]
  • drzaus
    drzaus about 9 years
    oy...what if your "backing field" is a method?
  • wiero
    wiero almost 9 years
    Which serializer do you use?
  • Kind Contributor
    Kind Contributor over 8 years
    NonSerialized will work for BinaryFormatter (at least) which serializes fields, and probably won't work for Json or XML serializers which serialize properties.
  • Antonio Nicolaas Teyken
    Antonio Nicolaas Teyken about 6 years
    I did not need to use [ScriptIgnore] to make this work
  • General Grievance
    General Grievance almost 5 years
    @AntonioNicolaasTeyken It depends on what you're using to serialize the data. In my case, [ScriptIgnore] prevented Json serialization.
  • Mass Dot Net
    Mass Dot Net over 4 years
    One more note: ScriptIgnoreAttribute is available in the System.Web.Script.Serialization namespace (MSDN).
  • Min
    Min about 4 years
    This should be re-marked as the best answer, because it's pretty awesome.
  • Corey Alix
    Corey Alix about 4 years
    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.