我可以阻止特定数据成员被反序列化吗?
我有一个像这样的数据合约
[DataContract]
class MyDC
{
[DataMember]
public string DM1;
[DataMember]
public string DM2;
[DataMember]
public string DM3;
}
,有时我想防止 DM2 在从操作合约返回时被反序列化。像这样的事情:
[OperationContact]
public MyDC GetMyDC()
{
MyDC mdc = new MyDC();
if (condition)
{
// Code to prevent DM2 from being deserialized
}
return mdc;
}
我总是可以创建一个仅包含 DM1 和 DM3 的新 DataContract 并从 MyDC 实例生成它,但我想看看是否可以以编程方式删除 DM2。是否可以?如何?
I have a datacontract like this
[DataContract]
class MyDC
{
[DataMember]
public string DM1;
[DataMember]
public string DM2;
[DataMember]
public string DM3;
}
and sometimes I want to prevent DM2 from being deserialized when being returned from an OperationContract. Something like this:
[OperationContact]
public MyDC GetMyDC()
{
MyDC mdc = new MyDC();
if (condition)
{
// Code to prevent DM2 from being deserialized
}
return mdc;
}
I could always make a new DataContract that has only DM1 and DM3 and generate that from the MyDC instance but I want to see if it is possible to programatically remove DM2. Is it possible? How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
执行此操作的一种方法是将 DataMemberAttribute 的 EmitDefaultValue 属性设置为 false:
然后将此属性设置为 null:
这样,该属性就不会在序列化时写入输出流。
http://msdn.microsoft.com/en-us/library/aa347792.aspx
One way to do this is to set the EmitDefaultValue property of the DataMemberAttribute to false:
Then setting this property to null:
This way, that property doesn't get written to the output stream on serialization.
http://msdn.microsoft.com/en-us/library/aa347792.aspx
你的意思是序列化而不是反序列化。
只有具有
[DataMember]
属性的类的成员才会被序列化:如果您准备一个用于序列化的类,并将
[DataContract]
属性应用于该类,则 复杂情况下使用[IgnoreDataMember]
可以解决您的问题。 (请参阅http://msdn.microsoft.com/en-us/library/ ms733127.aspx)顺便说一句,您可以序列化字段和属性,而不管可访问性如何:
private
、protected
、internal
、受保护的内部
或公共
。您可以序列化任何读/写属性,而不仅仅是字段。有关集合类型的序列化,请参阅 http://msdn.microsoft.com/en-我们/library/aa347850.aspx。What you mean is serialization and not deserialization.
If you prepare a class for serialization applying the
[DataContract]
attribute to the class, only the members of the class that has[DataMember]
attribute will be serialized:In some more complex cases the usage of
[IgnoreDataMember]
can solve your problem. (See http://msdn.microsoft.com/en-us/library/ms733127.aspx)By the way, you can serialize fields and properties, regardless of accessibility:
private
,protected
,internal
,protected internal
, orpublic
. You can serialize any read/write properties and not only fields. About serialization of collection types see http://msdn.microsoft.com/en-us/library/aa347850.aspx.然后在需要隐藏时将 IsDM2Serializable 设置为 false:
Then set IsDM2Serializable to false when you need to hide it:
是的,我们可以阻止属性序列化。
将
[DataContract]
注释放在类上,并将[DataMember]
仅用于序列化属性。如果您想在属性值为 null 时跳过该属性,请在该属性上放置[DataMember(EmitDefaultValue = false)]
。示例:
注意:SentOn 当不为空时将被序列化,其他的将在所有条件下被序列化。
Yes, we can prevent an attribute from serialization.
Put
[DataContract]
Annotation on class and[DataMember]
for only serialized attribute. if you want to skip attribute when that attribute value is null then put[DataMember(EmitDefaultValue = false)]
on that attribute.Example:
Note: SentOn will be serialized when it is not null and others will be serialized in every condition.