当我编写这样的代码时

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


推荐答案

好第一个错误是你不能这么做...

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

.net相关问答推荐

如何在AutoMapper中添加自定义方法到项目中?

在 F# 中处理 Option - Some(null) 的好策略是什么

如何在 Raspberry Pi 上托管 WASM 文件?

如何确定计时器是否正在运行?

如何让 DateTimePicker 显示一个空字符串?

如何以编程方式判断类型是 struct 还是类?

ReaderWriterLock 与锁{}

LINQ 性能常见问题解答

读取方法的属性值

无法加载文件或程序集'System.ComponentModel.Annotations,版本 = 4.1.0.0

如果需要,将方案添加到 URL

.net 自定义配置如何不区分大小写解析枚举 ConfigurationProperty

形成两个列表并集的最简单方法

.NET 的黄瓜替代品

log4net的正确使用方法(记录器命名)

如何修复 .NET Windows 应用程序在启动时崩溃并出现异常代码:0xE0434352?

带有嵌套控件的设计模式

使用 C# vs F# 或 F# vs C# 有什么好处?

.NET 桌面应用程序中的 Settings.settings 与 app.config

从 bcp 客户端接收到 colid 6 的无效列长度