以编程方式设置要从序列化中排除的属性 [英] Programmatically set properties to exclude from serialization

查看:39
本文介绍了以编程方式设置要从序列化中排除的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过编程方式设置您要从序列化中排除属性?

Is it possible to programmatically set that you want to exclude a property from serialization?

示例:

  • 反序列化时,我想加载一个ID字段
  • 序列化时,我不想输出ID字段

推荐答案

我相信这里有三个选择:

I believe there are three options here:

  1. 使用 XmlIgnore 属性.缺点是您需要事先知道要让xmlserializer忽略哪些属性.

  1. Use XmlIgnore attribute. The downside is that you need to know in advance which properties you want the xmlserializer to ignore.

实施 IXmlSerializable 界面.这样可以完全控制XML的输出,但是您需要自己实现读/写方法.

Implement the IXmlSerializable interface. This gives you complete control on the output of XML, but you need to implement the read/write methods yourself.

实现 ICustomTypeDescriptor 接口.我相信,无论您选择哪种序列化类型,这都将使您的解决方案正常工作,但这可能是所有解决方案中最长的解决方案.

Implement the ICustomTypeDescriptor interface. I believe this will make your solution to work no matter what type of serialization you choose, but it is probably the lengthiest solution of all.

 

这篇关于以编程方式设置要从序列化中排除的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆