如何使用DataContractSerializer序列化基类? [英] How to serialize the base class with DataContractSerializer?

查看:67
本文介绍了如何使用DataContractSerializer序列化基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我很荒谬,但是我不知道为什么Microsoft在DataContract中拥有KnownTypes.  

什么我想做的只是将我的大类序列化为XML,XMLSerilizer仅适用于公共读写属性,而Microsoft 会放弃这一点,所以我转向了DataContract.我从此处读取/测试了KnownType属性.

What I want to do is simply serialize my big class to XML, XMLSerilizer only works with public read/write properties and Microsoft would abandon that, so I turned to DataContract. I read/test the KnownType attribute from here. 

http://msdn.microsoft.com/zh-cn/library/ms730167.aspx

通常它说DataContract不能序列化一个子类,除非将其设置为KnowType.这是上面链接中的一个示例,所以我 有4个班级,

Generally it said DataContract cannot serialize a sub class unless it is set as KnowType. Here is a sample in the above link, so I have 4 classes, 

[DataContract]

[DataContract]

公共 class

public class Shape { }

[DataContract(Name = 圆圈"

[DataContract(Name = "Circle")]

公共 class

public class CircleType : Shape { }

[DataContract(Name = 三角形"

[DataContract(Name = "Triangle")]

公共 class

public class TriangleType : Shape { }

[DataContract]

public class

public class CompanyLogo

{

    [DataMember]

    [DataMember]

    私有

    private Shape ShapeOfLogo;

    [DataMember]

    [DataMember]

    私有 int

    private int ColorOfLogo;

}

 

推荐答案

关于WCF的事情是,它旨在使您能够开发基于SOA的应用程序,这些应用程序的风格并不总是面向对象的.

The thing about WCF is that it is intended to enable you to develop SOA based applications which are not always object-oriented in style.

对以下内容进行映像-一个Java客户端现在使用您的服务,它获取您实现的数据协定的列表.它调用该服务,得到一个圆,并将其反序列化为它自己的圆".类型.现在,您将椭圆类添加到服务中-您的客户端 对此一无所知.它调用该服务,并获得一个用于椭圆的XML-它应如何处理?不是圆形或矩形?

Image the following - a java client uses your service now, and it gets the list of data contract you implement. It calls the service, gets a circle and deserialize it into it's own "circle" type. Now you add an ellipse class to your service - you client doesn't know about this. It calls the service and it gets an XML for ellipse - what should it do with it? it's not a circle or rectangle?

多态性的问题是,只要您的客户端具有所有可能的类的列表,就可以了,但是当您将新类添加到服务的输出中时,您的客户端就不知道了-这是不好.

The issue with polymorphism is that as long as your client has a list of all possible classes, it is ok, but when you add a new class to your service's output and your clients don't know about this - this is bad.

但是...您可以创建一个knownType方法,该方法将扫描应用程序以查找形状的每个派生类型并构建已知类型列表.如果查看knowntype属性,您将看到它接受方法的名称作为参数,并且 该方法返回派生类型的列表:

But... you can create a knownType method that scans the application in order to find each derived type of your shape and build the known types list. If you look at the knowntype attribute, you will see it accepts the name of a method as a parameter, and that methods returns a list of derived types:

http://msdn.microsoft.com/en-us/library/ms584302.aspx

当然,每次添加新类时,这仍然需要客户端进行更新,否则客户端将不知道如何处理此返回值.如果您有.net客户端,则可以使用您的服务合同来代替使用服务引用 汇编并立即知道每种派生类型.

Of course this will still require clients to get updated every time you add a new class, otherwise they won't know what to do with this return value. If you have .net clients, then instead of using a service reference, they can reference your service's contract assembly and immediatelly know about each derived type.


这篇关于如何使用DataContractSerializer序列化基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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