IAd*_*ter 56 .net c# serialization properties
当我写这样的代码
[XmlIgnore]
[NonSerialized]
public List<string> paramFiles { get; set; }
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Attribute 'NonSerialized' is not valid on this declaration type.
It is only valid on 'field' declarations.
Run Code Online (Sandbox Code Playgroud)
如果我写
[field: NonSerialized]
Run Code Online (Sandbox Code Playgroud)
我收到以下警告
'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.
Run Code Online (Sandbox Code Playgroud)
如果我写
[property: NonSerialized]
Run Code Online (Sandbox Code Playgroud)
我再次收到以下错误:
Attribute 'NonSerialized' is not valid on this declaration type.
It is only valid on 'field' declarations.
Run Code Online (Sandbox Code Playgroud)
我怎样才能[NonSerialized]
在物业上使用?
Ant*_*rko 69
使用简单:
[XmlIgnore]
[ScriptIgnore]
public List<string> paramFiles { get; set; }
Run Code Online (Sandbox Code Playgroud)
希望它有所帮助.
wie*_*ero 49
嗯......第一个错误说你不能这样做......来自http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx
[AttributeUsageAttribute(AttributeTargets.Field, Inherited = false)]
[ComVisibleAttribute(true)]
public sealed class NonSerializedAttribute : Attribute
Run Code Online (Sandbox Code Playgroud)
我建议使用支持领域
public List<string> paramFiles { get { return list;} set { list = value; } }
[NonSerialized]
private List<string> list;
Run Code Online (Sandbox Code Playgroud)
小智 31
从 C# 7.3 开始,您可以将属性附加到自动实现的属性的支持字段。
因此,如果您将项目的语言更新为 C# 7.3,以下内容应该有效:
[field: NonSerialized]
public List<string> paramFiles { get; set; }
Run Code Online (Sandbox Code Playgroud)
对于使用 JSON 而不是 XML 的用户,您可以在[JsonIgnore]
属性上使用该属性:
[JsonIgnore]
public List<string> paramFiles { get; set; }
Run Code Online (Sandbox Code Playgroud)
在Newtonsoft.Json和System.Text.Json (.NET Core 3.0) 中可用。
从 .NET 3.0 开始,您可以使用DataContract而不是 Serialized。不过,对于 DataContract,您需要通过使用DataMember属性标记可序列化字段来“选择加入”;或者通过使用 DataMember 属性来“选择加入”。或使用IgnoreDataMember “选择退出” 。
选择加入与选择退出之间的主要区别在于,选择退出默认情况下只会序列化公共成员,而选择加入只会序列化标记的成员(无论保护级别如何)。
归档时间: |
|
查看次数: |
82579 次 |
最近记录: |