NonSerialized on property
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).
IAdapter
Updated on October 29, 2020Comments
-
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. over 11 yearsUsing the suggested backing field does not seem to work. The property is still being serialized.
-
wiero over 11 yearsYes it works, but not in every class named AaaSerializer. Which serializer are you using?
-
timmi4sa over 11 yearsIndeed 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 almost 11 yearsThe correct attribute is [NonSerialized], not [NonSerializable]
-
drzaus about 9 yearsoy...what if your "backing field" is a method?
-
wiero almost 9 yearsWhich serializer do you use?
-
Kind Contributor over 8 yearsNonSerialized 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 about 6 yearsI did not need to use [ScriptIgnore] to make this work
-
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 over 4 yearsOne more note:
ScriptIgnoreAttribute
is available in theSystem.Web.Script.Serialization
namespace (MSDN). -
Min about 4 yearsThis should be re-marked as the best answer, because it's pretty awesome.
-
Corey Alix about 4 yearsJust to be clear, this works for binary serializer? Because the question already has XmlIgnore in the example so not sure how this can help.