为什么XmlSerializer的序列化抽象类而不是接口? [英] Why can XmlSerializer serialize abstract classes but not interfaces?

查看:255
本文介绍了为什么XmlSerializer的序列化抽象类而不是接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改:此code应该说明整个问题:

Edit This code should illustrate the whole problem:

[XmlInclude(typeof(AThing1))]
public abstract class AThing
{
    public abstract string Name { get; set; }
}

[XmlInclude(typeof(IThing1))]
public interface IThing
{
    string Name { get; set; }
}

public class AThing1 : AThing
{
    public override string Name { get; set; }
}

public class IThing1 : IThing
{
    public string Name { get; set; }
}

List<AThing> aThings = new List<AThing>(new AThing[] { new AThing1() { Name = "Bob" } });
List<IThing> iThings = new List<IThing>(new IThing[] { new IThing1() { Name = "Bob" } });

public void Test()
{
    using (StringWriter sw = new StringWriter())
    {
        XmlSerializer aSerializer = new XmlSerializer(typeof(List<AThing>));
        aSerializer.Serialize(sw, aThings);
        string text = sw.ToString();
    }

    using (StringWriter sw = new StringWriter())
    {
        // This line will throw "Cannot serialize interface IThing.":
        XmlSerializer iSerializer = new XmlSerializer(typeof(List<IThing>));    
        iSerializer.Serialize(sw, iThings);
        string text = sw.ToString();
    }
}

第一个文本 aSerializer产生将是:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfAThing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <AThing xsi:type="AThing1">
    <Name>Bob</Name>
  </AThing>
</ArrayOfAThing>

我不明白为什么不能 iSerializer 做到这一点:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfIThing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <IThing xsi:type="IThing1">
    <Name>Bob</Name>
  </IThing>
</ArrayOfIThing>

而不是抛出异常。

instead of throwing an exception.

推荐答案

您可以序列化的界面,但不是那么简单的类:

you can serialize interface, but not as simple as classes:

<一个href="http://ventspace.word$p$pss.com/2010/02/20/how-to-serialize-interfaces-in-net/">http://ventspace.word$p$pss.com/2010/02/20/how-to-serialize-interfaces-in-net/

但要回答你的问题我有2猜测这个:

but to answer your question I have 2 guesses on this:

第一个原因是从实用性的一面;序列化的语义 接口都有点模糊。你怎么认为你对串行 应该序列当你传递一个接口的参考?如果你只 序列化接口属性的反序列化,然后可以与风 中途未初始化对象。有没有说什么就做 您的应用程序。

The first reason is from the practical side; The semantics of serializing an interface are a little bit blurry. What do you you think the serializer should serialize when you pass in an interface reference ? If you only serialize the interface properties your deserialize then could wind up with a half-way uninitialized object. There's no telling what that would do to your application.

如果您一起类型信息,然后在序列完整的对象 序列化的界面真的没有买任何东西。你可以键入 参考作为摆在首位,如果真的是你的应用程序中的类的类型 在乎对象是存在的。

If you serialize the full object together with the type information then serializing the interface really did not buy you anything. You could type the reference as a class type in the first place if your application really cares what object is there.

第二个去与XmlSerializer的的既定目的。尽管 在.NET Framework中的误导名称XML序列化确实是一个数据 结合技术的主要意图映射定义MXL数据类型 在XSD架构以.NET类型。该XSD定义知道抽象基 班,但因为它是数据中心,不知道什么 接口。考虑到这一点很少有动力支持 在接口XmlSerializer的。

The second one goes with the stated purpose of the XmlSerializer. Despite the misleading name XML Serialization in the .NET Framework really is a data binding technology with the primary intention to map MXL data types defined in XSD schemas to .NET types. The XSD definition knows about abstract base classes, but since it's data centric, does not know anything about interfaces. With that in mind there is little motivation to support interfaces in the XmlSerializer.

这篇关于为什么XmlSerializer的序列化抽象类而不是接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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