XmlSerializer的,"指定"后缀IReflect [英] XmlSerializer, "Specified" suffix and IReflect

查看:115
本文介绍了XmlSerializer的,"指定"后缀IReflect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,如果一个序列化的字段/属性的有类型的相应字段的布尔的有一个名称的字段/属性的与指定的后缀名,XmlSerializer的有条件排除的字段/属性的从序列化进程。太好了!



所以,我想避免这些字段的定义,并将其添加动态的,在运行时...



我发现一个有趣的界面 IReflect ,我可以用它来模拟,这是使用XmlSerializer的实例排除某些领域的动态领域。



将这工作?


< DIV CLASS =h2_lin>解决方案

如果你想利用你的XML序列化的控制,那么你有两个选择。第一次(这可能不适合在这里)它使用的属性在System.Xml.Serialization命名空间来排除特性。如果你真的需要做决定什么都在运行时序列化,这可能不是最好的行动过程。



请参阅的属性,控制XML序列化



另一种方式做,这是实现你的类IXmlSerializable接口和实施的ReadXml和方法中WriteXML。这使您可以充分利用你的XML究竟如何看的控制。欲了解更多信息这个问题:



自定义XML序列化



不过,这里提到的混合习惯和基本序列化?一旦你实现IXmlSerializable的,你有责任为所有类型的序列化逻辑。


I've discovered that if a serializable Field/Property has a corresponding field of type Boolean having as a name the Field/Property name with "Specified" suffix, the XmlSerializer conditionally exclude that Field/Property from the serialization process. Nice!

So, I want to avoid the definition of those fields, and add them dynamically, at runtime...

Reading this, I found an interesting interface IReflect, which I can use to "emulate" dynamic fields which are used by the XmlSerializer instances to exclude certain fields.

Would this work?

解决方案

If you want to take control of your xml serialization then you have two options. The first (which might not be appropriate here) it to use the attributes in the System.Xml.Serialization namespace to exclude properties. If you really need to do determine what gets serialized at runtime this might not be the best course of action.

See Attributes That Control XML Serialization

The other way to do this is to implement the IXmlSerializable interface on your class and implement the ReadXml and WriteXml methods. This allows you to take control of exactly how your xml looks. See this question for additional info:

custom xml serialization

However, as mentioned here Mixing custom and basic serialization? once you implement IXmlSerializable you are responsible for all the serialization logic for your type.

这篇关于XmlSerializer的,&QUOT;指定&QUOT;后缀IReflect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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