非序列化的属性 [英] NonSerialized on property

查看:533
本文介绍了非序列化的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我写code这样

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

推荐答案

嗯......第一个错误告诉你不能.. 从<一个href="http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx">http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx

well... first error tell that you can't.. 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;

这篇关于非序列化的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆