当我写这样的代码时

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

我收到以下错误(再次):
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

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

关于c# - 属性上的非序列化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7693391/

10-13 07:07