2011-10-07 12 views
39

Cuando escribo código como esteNonSerialized en la propiedad

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

me sale el siguiente error:

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


Si escribo

[field: NonSerialized] 

me sale el siguiente aviso

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


Si escribo

[property: NonSerialized] 

me sale el siguiente error (de nuevo):

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


¿Cómo puedo usar [NonSerialized] en una propiedad?

+0

Posible duplicado de [¿Cómo marcar una propiedad como no serializable para json?] (Http://stackoverflow.com/questions/5103200/how-to-mark-a-property-as-non-serializable-for- json) –

Respuesta

42

así ... primer error diga que no puede .. de http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx

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

sugiero campo de respaldo utilizando

public List<string> paramFiles { get { return list;} set { list = value; } } 
[NonSerialized] 
private List<string> list; 
+9

El uso del campo de respaldo sugerido no parece funcionar. La propiedad aún se está serializando. –

+0

Sí, funciona, pero no en todas las clases denominadas AaaSerializer. ¿Qué serializador estás usando? – wiero

+2

El atributo correcto es [NonSerialized], not [NonSerializable] –

52

uso simple:

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

Con suerte, ayuda.

+2

De hecho, esto incluso funciona en el clásico escenario del Servicio Web ASP.NET ocultando por completo el nombre de la propiedad del consumidor (un gran truco para los que se quedaron atrapados en el proyecto pre-WCF) – timmi4sa

+0

muchas gracias que es perfecto – Mik1893

+0

No necesité usar [ScriptIgnore] para hacer que esto funcione –

Cuestiones relacionadas