在实施的IXmlSerializable,如何只覆盖要么的ReadXml或与中WriteXML不是两者兼而有之? [英] When implementing IXmlSerializable, how to only override either ReadXml or WriteXml and not both?

查看:206
本文介绍了在实施的IXmlSerializable,如何只覆盖要么的ReadXml或与中WriteXML不是两者兼而有之?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的IXmlSerializable类上,只覆盖任何的ReadXml或中WriteXML,但不能同时使用。如果我没有在这个类实现IXmlSerializable的,XmlSerializer的会自动序列化的所有成员。我想是默认行为申请非重写案。但是,由于的IXmlSerializable是一个接口,而不是一个基类,我不知道如何去说。

I would like to implement IXmlSerializable on a class and only override either ReadXml or WriteXml, but not both. If I didn't implement IXMLSerializable on this class, the XMLSerializer would automatically serialize all members. I'd like that default behavior to apply for the non-overridden case. However, since IXmlSerializable is an interface, and not a base class, I'm not sure how to go about that.

另外,我需要,在一种情况下,这样做的默认行为,并且该操作完成时添加一些额外的code。所以,我想重写,回拨到'基地'类的行为。同样,这将是微不足道,如果有一个基类,但并非如此微不足道,因为这是一个接口

In addition, I need to, in one case, do the default behavior, and when that is complete add some extra code. So, I'd like to override and call back to the 'base' class behavior. Again, this would be trivial if there was a base class, but not so trivial since this is an interface.

谢谢!

推荐答案

在执行操作的能力/序列化后/ deserialisation具备的属性。马克的方法与<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx"相对=nofollow> OnDeserializedAttribute 它被称为后实例已经deserialised,只是确保该方法具有正确的签名:

The ability to perform actions before/after serialisation/deserialisation is provided with attributes. Mark a method with OnDeserializedAttribute for it to be called after an instance has been deserialised, just ensure the method has the right signature:

[OnDeserializedAttribute()]
private void RunThisMethod(StreamingContext context) { 
  // ...
}

NB。此属性适用于二进制,SOAP和数据属性格式化,但是的没有的用于XmlSerializer的。没有属性或机制比实现的IXmlSerializable。

NB. This attribute works for Binary, SOAP and DataAttribute formatters, but not for XmlSerializer. There is no attribute or mechanism other than implementing IXmlSerializable.

另外不要忘记,你可以直接读取XML文件,并写入code到(德)序列化。

Also do not forget that you can read XML documents directly and write code to (de)serialize.


原来的答复: 如果您需要完全重写序列化或deserialisation之一(因而实施的IXmlSerializable ,那么你必须自己做两个。

Original answer: If you need to completely override one of serialisation or deserialisation (and thus implement IXmlSerializable then you have to do both yourself.

这也许可以利用属性和其他机制来避免使用的IXmlSerializable ,你能不能扩大与中的为什么的你的细节问题需要实现的只有一个的ReadXml 中WriteXML

It may be possible to make use of attributes and other mechanisms to avoid using IXmlSerializable, could you expand the question with details of why you need to implement just one of ReadXml or WriteXml.

这篇关于在实施的IXmlSerializable,如何只覆盖要么的ReadXml或与中WriteXML不是两者兼而有之?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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