English 中文(简体)
为什么XmlSerializer能够把抽象的班次编成序,而不是连接起来?
原标题:Why can XmlSerializer serialize abstract classes but not interfaces?

<><>Edit>/strong> 该法典应说明整个问题:

[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生成的第一个<代码>text将:

<?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>

而不是放弃例外。

最佳回答

您可以编订接口,但不能简单地按类别分类:

http://ventspace.wordpress.com/ 201002/20/how-to-serialize-interfaces-in-net/。

回答你的问题,我有两点:

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.

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.

问题回答

The XmlSerializer is not serializing abstract classes. It is serializing one of several concrete classes.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签