C# 如何防止自动实现的属性被序列化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1728367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prevent auto implemented properties from being serialized?
提问by hopla
How can I prevent a auto implemented property from being serialized by the binary formatter? The [NonSerialized] attribute can only be used with fields. And the field is hidden when using auto implemented properties.
如何防止自动实现的属性被二进制格式化程序序列化?[NonSerialized] 属性只能与字段一起使用。使用自动实现的属性时,该字段是隐藏的。
采纳答案by Jehof
It′s not supported for auto implemented properties. You have to use a backing field and set the NonSerializedAttributeon it.
它不支持自动实现的属性。您必须使用支持字段并在其上设置NonSerializedAttribute。
public class ClassWithNonSerializedProperty {
[NonSerialized]
private object _data; // Backing field of Property Data that is not serialized
public object Data{
get { return _data; }
set { _data = value; }
}
}
回答by Danil
If you are serializing to Xml then you can use XmlIgnore attribute.
如果您要序列化为 Xml,则可以使用 XmlIgnore 属性。
回答by Neil Barnwell
I'm not sure you can. This MSDN article on SerializableAttribute
suggests you implement ISerializable and control the serialisation yourself:
我不确定你能不能。这篇MSDN 文章SerializableAttribute
建议您实现 ISerializable 并自己控制序列化:
All the public and private fields in a type that are marked by the SerializableAttribute are serialized by default, unless the type implements the ISerializable interface to override the serialization process.
默认情况下,由 SerializableAttribute 标记的类型中的所有公共和私有字段都会被序列化,除非该类型实现了 ISerializable 接口来覆盖序列化过程。
Or switch away from an auto-property for that specific field.
或者从该特定字段的自动属性切换。
回答by xyz
// This works for the underlying delegate of the `event` add/remove mechanism.
[field:NonSerialized]
public event EventHandler SomethingHappened;
But it doesn't seem to for auto-implemented properties. I thought it was worth mentioning because it's useful to know when serializing an object with event subscribers attached to it.
但它似乎不适用于自动实现的属性。我认为这是值得一提的,因为知道何时序列化带有事件订阅者的对象很有用。
回答by Sergey Teplyakov
It's not possible for auto implemented properties. Consider folowing:
自动实现的属性是不可能的。考虑以下:
This behavior is "by design". The decision at the time auto-properties were implemented was that they would work in the "common case" which among other things means no attributes on the generated field. The idea behind that is keeping them simple and not slowly mutating them into full properties. So, if you need to use the NonSerialized attribute full properties are the way.
此行为是设计使然”。实现自动属性时的决定是它们将在“常见情况”下工作,这意味着生成的字段上没有属性。这背后的想法是让它们保持简单,而不是慢慢地将它们变异成完整的属性。所以,如果你需要使用NonSerialized 属性的完整属性是这样的。
回答by LRaiz
The proposed solution of using not-serialized backing field does not seem to function properly with .NET 4.0 (at least in the case of Xml serialization). The field indeed does not get serialized but public property that uses it does serialize and thus defeats the purpose. Using XmlIgnore workaround helps in case of Xml serialization. Disclaimer - I did not verify binary serialization behavior.
建议的使用非序列化支持字段的解决方案在 .NET 4.0 中似乎无法正常运行(至少在 Xml 序列化的情况下)。该字段确实没有被序列化,但使用它的公共财产确实序列化并因此违背了目的。在 Xml 序列化的情况下,使用 XmlIgnore 解决方法会有所帮助。免责声明 - 我没有验证二进制序列化行为。
回答by Bern
If you're serializing to Json and using the Json.NET serializer(which I'd highly recommend as it has a lot more to offer than a number of other serializers on the market) then you can achieve your desired outcome on properties using [JsonIgnore]
.
如果您要序列化为 Json 并使用Json.NET 序列化程序(我强烈推荐它,因为它比市场上的许多其他序列化程序提供更多功能),那么您可以使用[JsonIgnore]
.
You don't need to create a field.
您不需要创建字段。
So your code would be:
所以你的代码是:
public class ClassName
{
[JsonIgnore]
public object IgnoreMe { get; set; }
public object DoNotIgnoreMe { get; set; }
}