如何防止自动实现的属性被序列化?

发布于 2024-08-10 21:53:25 字数 81 浏览 1 评论 0原文

如何防止自动实现的属性被二进制格式化程序序列化? [NonSerialized] 属性只能与字段一起使用。当使用自动实现的属性时,该字段会被隐藏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

寒冷纷飞旳雪 2024-08-17 21:53:25

自动实现的属性不支持它。您必须使用支持字段并在其上设置 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; }
  }
}

It´s not supported for auto implemented properties. You have to use a backing field and set the NonSerializedAttribute on it.

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; }
  }
}
善良天后 2024-08-17 21:53:25
// This works for the underlying delegate of the `event` add/remove mechanism.
[field:NonSerialized]
public event EventHandler SomethingHappened;

但它似乎不适用于自动实现的属性。我认为这是值得一提的,因为知道何时序列化带有附加事件订阅者的对象非常有用。

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

中性美 2024-08-17 21:53:25

如果您要序列化为 Json 并使用 Json.NET 序列化器(我强烈推荐它,因为它具有比市场上的许多其他序列化器提供更多功能),然后您可以使用 [JsonIgnore] 在属性上实现您想要的结果。

您不需要创建字段。

所以你的代码将是:

public class ClassName
{
     [JsonIgnore]   
     public object IgnoreMe { get; set; } 

     public object DoNotIgnoreMe { get; set; } 
}

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

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; } 
}
岁月无声 2024-08-17 21:53:25

如果要序列化为 Xml,则可以使用 XmlIgnore 属性。

If you are serializing to Xml then you can use XmlIgnore attribute.

且行且努力 2024-08-17 21:53:25

我不确定你可以。这篇关于 SerializedAttribute 的 MSDN 文章建议您实现 ISerialized 并自己控制序列化:

默认情况下,类型中由 SerializedAttribute 标记的所有公共和私有字段都会序列化,除非该类型实现 ISerialized 接口来覆盖序列化过程。

或者放弃该特定字段的自动属性。

I'm not sure you can. This MSDN article on SerializableAttribute suggests you implement ISerializable and control the serialisation yourself:

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.

Or switch away from an auto-property for that specific field.

又爬满兰若 2024-08-17 21:53:25

自动实现的属性是不可能的。
考虑以下内容:

此行为是“设计使然”。当时的决定是auto-property
实施的一点是它们将在“常见情况”下工作
除其他外,意味着生成的字段上没有属性。这
其背后的想法是让它们保持简单,而不是慢慢地改变它们
变成完整的属性。因此,如果您需要使用 NonSerialized
attribute 完整属性就是这样。

(http://social .msdn.microsoft.com/Forums/en-US/vcsharp2008prerelease/thread/2b6eb489-122a-4418-8759-10808252b195

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.

(http://social.msdn.microsoft.com/Forums/en-US/vcsharp2008prerelease/thread/2b6eb489-122a-4418-8759-10808252b195)

不喜欢何必死缠烂打 2024-08-17 21:53:25

建议的使用非序列化支持字段的解决方案似乎无法在 .NET 4.0 中正常运行(至少在 Xml 序列化的情况下)。该字段确实没有被序列化,但使用它的公共财产确实被序列化,从而达不到目的。使用 XmlIgnore 解决方法有助于解决 Xml 序列化问题。免责声明 - 我没有验证二进制序列化行为。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文