C# 使用 DataContractSerializer 进行序列化时如何忽略属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1791946/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:52:23  来源:igfitidea点击:

How can I ignore a property when serializing using the DataContractSerializer?

c#.netwcfserialization

提问by NotDan

I am using .NET 3.5SP1 and DataContractSerializer to serialize a class. In SP1, they changed the behavior so that you don't have to include DataContract/DataMember attributes on the class and it will just serialize the entire thing. This is the behavior I am using, but now I need to ignore one property from the serializer. I know that one way to do this is to add the DataContract attribute to the class, and just put the DataMember attribute on all of the members that I want to include. I have reasons, though, that this will not work for me.

我正在使用 .NET 3.5SP1 和 DataContractSerializer 来序列化一个类。在 SP1 中,他们更改了行为,因此您不必在类中包含 DataContract/DataMember 属性,它只会序列化整个事物。这是我正在使用的行为,但现在我需要忽略序列化程序中的一个属性。我知道这样做的一种方法是将 DataContract 属性添加到类,并将 DataMember 属性放在我想要包含的所有成员上。不过,我有理由认为这对我不起作用。

So my question is, is there an attribute or something I can use to make the DataContractSerializer ignore a property?

所以我的问题是,是否有一个属性或什么东西可以用来让 DataContractSerializer 忽略一个属性?

回答by Tony The Lion

What you are saying is in conflict with what it says in the MSDN library at this location:

您所说的与此位置的 MSDN 库中所说的内容相冲突:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx

I don't see any mention of the SP1 feature you mention.

我没有看到您提到的 SP1 功能的任何提及。

回答by Doug

Additionally, DataContractSerializer will serialize items marked as [Serializable] and will also serialize unmarked types in .NET 3.5 SP1 and later, to allow support for serializing anonymous types.

此外,DataContractSerializer 将序列化标记为 [Serializable] 的项目,还将序列化 .NET 3.5 SP1 及更高版本中未标记的类型,以支持序列化匿名类型。

So, it depends on how you've decorated your class as to how to keep a member from serializing:

因此,这取决于您如何装饰类以防止成员序列化:

  • If you used [DataContract], then remove the [DataMember]for the property.
  • If you used [Serializable], then add [NonSerialized]in front of the fieldfor the property.
  • If you haven't decorated your class, then you should add [IgnoreDataMember]to the property.
  • 如果您使用了[DataContract],则删除[DataMember]该属性的 。
  • 如果您使用[Serializable],则[NonSerialized]在该属性的字段前添加。
  • 如果你还没有装饰你的类,那么你应该添加[IgnoreDataMember]到属性中。

回答by Cris Valenzuela

Try marking the field with [NonSerialized()] attribute. This will tell the serializer to ignore the field.

尝试使用 [NonSerialized()] 属性标记该字段。这将告诉序列化程序忽略该字段。

https://msdn.microsoft.com/en-us/library/system.nonserializedattribute(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.nonserializedattribute(v=vs.110).aspx

回答by Kris Adams

In XML Serializing, you can use the [XmlIgnore] attribute (System.Xml.Serialization.XmlIgnoreAttribute) to ignore a property when serializing a class.

在 XML 序列化中,您可以使用 [XmlIgnore] 属性 (System.Xml.Serialization.XmlIgnoreAttribute) 在序列化类时忽略属性。

This may be of use to you (Or it just may be of use to anyone who found this question when attempting to find out how to ignore a property when Serializing in XML, as I was).

这可能对你有用(或者它可能对任何在尝试找出在 XML 中序列化时如何忽略属性时发现这个问题的人有用,就像我一样)。