的IXmlSerializable和继承 [英] IXmlSerializable and inheritance

查看:148
本文介绍了的IXmlSerializable和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我有两个班,一个基类和派生类。他们是相当简单的类,主要只是作为数据结构(与派生类显然是稍微复杂一些)。

Let's say that I have two classes, a base class and a derived class. They are fairly simple classes, and mainly just act as data structures (with the derived class obviously being slightly more complex).

public class BaseUserSession
{
    // ... Various properties ...
}

public class DerivedUserSession : BaseUserSession
{
    // ... Even more properties ...
}

这些类需要被序列化到XML。现在,这可以简单地通过指定 [Serializable接口] 上面的类声明,默认XML序列化效果很好。

These classes need to be serialized into XML. Now, this can be done simply by specifying [Serializable] above the class declarations, and the default XML serializer works well.

但在现实中,也有从基类继承多个类。我的目标是能够以具有任何派生类的序列化的XML是由基类消耗品

In reality, however, there are multiple classes that inherit from the base class. My goal is to be able to have the serialized XML of any of the derived classes be consumable by the base class.

我曾尝试两种方法,这两者都不工作的当前。首先,使用本地XML串行化器和基类中的某些控制的属性。生成的XML看起来是这样的:<?/ p>

I have tried two methods, neither of which work currently. First, using the native XML serializer and some control attributes within the base class. The generated XML looks something like this:

<?xml version="1.0" encoding="utf-16"?>
<DerivedUserSession xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SSO="www.mywebsite.com">
    <SSO:Id />
    <SSO:Authenticated>false</SSO:Authenticated>
    <SSO:Role />
    <EmailAddress />
    <FullName />
    <Street />
    <City />
    <State />
    <Zip />
    <DayPhone />
    <EveningPhone />
    <ErrorMessage />
    <Birthdate>0001-01-01T00:00:00</Birthdate>
    <CurrentControl>0</CurrentControl>
</DerivedUserSession>

这是前缀 SSO 的标签从基类继承的的人。但是,当我试图反序列化到一个 BaseUserSession 对象这一点,它抛出一个异常: System.InvalidOperationException:其中,DerivedUserSession的xmlns =''>是没有预料到。

The tags that are prefixed with SSO are the ones that were inherited from the base class. But when I attempt to deserialize this into a BaseUserSession object, it throws an exception: System.InvalidOperationException: <DerivedUserSession xmlns=''> was not expected.

我也试图使基类的IXmlSerializable ,阅读并手动编写所有的XML。但是,派生类无法写出自己的属性,我真的不希望实施的IXmlSerializable 每类。

I also tried making the base class IXmlSerializable, and reading and writing all of the XML manually. However, the derived classes fail to write out their properties, and I really don't want to implement IXmlSerializable for every class.

有没有更简单的方式来做到这一点?

Is there an easier way to do this?

=编辑=

我终于实现了一个相当丑陋的方式做到这一点。这两个类从基继承,他们都实施的IXmlSerializable 。每个类(含底座)写出其字段值(精心...)到它自己的使用XML层次反思,以及读取这些值回。写入的第一值始终从基类

I have finally implemented a fairly ugly method to accomplish this. The two classes inherit from the base, and they all implement IXmlSerializable. Each class (including the base) writes out its field values (painstakingly...) into its own XML hierarchy using reflection, as well as reads those values back. The first values written are always from the base class.

每个应用程序还包括用于管理串行化为XML和背面码,提供一个简单的界面。会议本身并不多,现在不是简单地接受XML序列化,并将其存储在一个共同的关键做任何事情。序列化和从XML的所有应用程序处理,并共同 XmlRootAttribute 在所有应用中被用来绕过出现InvalidOperationException

Each application also includes code that manages the serialization into XML and back, providing an easy interface. The session itself doesn't do anything more now than simply accept the serialized XML and store it under a common key. The serialization into and from XML is all handled by the application, and a common XmlRootAttribute is used in all applications to bypass the InvalidOperationException.

所以,现在,当另一个应用程序试图反序列化最初是在不同的应用程序中创建XML,它管理通过公共基类值它击中前必读一个无法识别的元素,此时它会和其他地方获取所需的价值观,这是可能的,因为它已经拥有了共同的基础值。

So now, when another application tries to deserialize XML that was initially created in a different application, it manages to read through the common base class values before it hits an unrecognized element, at which point it goes and fetches the needed values elsewhere, which is possible because it will already have the common base values.

我需要阅读了上系列化原则...

I need to read up on serialization principles...

推荐答案

XmlSerialization是不是多态(成员属性,然而,去与声明类,也就是说,除非你否决的关键字的成员(与覆盖),你会'继承'序列化的属性。

XmlSerialization is not polymorphic (the member attributes, however, go with the declaring class; i.e. unless you overrule a member with the new keyword (in contrast to override) you'll 'inherit' the serialization attributes.

反序列化结果总是在连载的实际类型。这使得很多的感觉,真的。

Deserialization always results in the actual type that was serialized. This makes a lot of sense, really.

这篇关于的IXmlSerializable和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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