gpt4 book ai didi

c# - 将 "NonSerializedAttribute"设置为自动属性

转载 作者:IT王子 更新时间:2023-10-29 04:44:16 26 4
gpt4 key购买 nike

这不能在 C# 中完成。有什么办法吗?

...

,以防我的小双关语没被理解,我的意思是:如何在 C# 中将属性标记为非序列化?当然,当属性包含逻辑时,自然无法做到这一点,但自动属性是可序列化的,因此,我希望有一些方法可以让我阻止它们的序列化。

最佳答案

  [NonSerialized]
public string MyProperty { get; set; }

是一个错误

  [XmlIgnore]
public string MyProperty { get; set; }

不是错误

NonSerialized Indicates that a field of a serializable class should not be serialized.

XmlIgnore Instructs the Serialize method of the XmlSerializer not to serialize the public field or public read/write property value

所以,如果你问

I would expect to have some way to allow me to prevent their serialization.

答案是肯定的,如果你使用 XmlSerializer

关于c# - 将 "NonSerializedAttribute"设置为自动属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4184680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com