NonSerialized 属性忽略序列化报错'NonSerialized' is not valid on this declaration type

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

 

 
 
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;

 

参考:https://stackoverflow.com/questions/7693391/nonserialized-on-property

 

延伸:https://www.cnblogs.com/shy1766IT/p/5459707.html    Json序列化指定输出字段 忽略属性

posted @ 2019-08-08 06:49  BloggerSb  阅读(464)  评论(0编辑  收藏  举报