Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NonSerialized on property

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?

like image 969
IAdapter Avatar asked Oct 07 '11 22:10

IAdapter


People also ask

How can you prevent a property from being serialized?

You can prevent member variables from being serialized by marking them with the NonSerialized attribute as follows. If possible, make an object that could contain security-sensitive data nonserializable. If the object must be serialized, apply the NonSerialized attribute to specific fields that store sensitive data.

How to not serialize a property 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.

What does non serialized mean?

Non-serialized means more than just an item that does not have a serial number, if you are receiving items without a serial number you should review the differences below and decide if you want to use serialized or non-serialized.

What is serialization used for?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.


1 Answers

Simple use:

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

Hopefully, it helps.

like image 80
Anton Norko Avatar answered Oct 04 '22 09:10

Anton Norko