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

问题描述

当我写这样的代码时

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

我收到以下错误:

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


如果我写

[field: NonSerialized]

我收到以下警告

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


如果我写

[property: NonSerialized]

我收到以下错误(再次):

I get the following error (again):

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


如何在属性上使用 [NonSerialized]?

推荐答案

嗯……第一个错误说你不能那样做……来自 http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx

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

我建议使用支持字段

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

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

10-13 12:49