为什么KnownTypeAttribute需要WCF [英] Why KnownTypeAttribute need in WCF

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

问题描述

我学习WCF和不理解KnowTypeAttribute的真正优势。可能有人解释我只是我们为什么需要它?

I am learning WCF and do not understand the real advantage of KnowTypeAttribute. Could somebody explain me simply why we need it?

推荐答案

KnownTypeAttribute让您指定接受派生类中对于给定的数据合同。它指定应该由DataContractSerializer的序列或反序列化给定类型时识别类型。

KnownTypeAttribute enable you to designate acceptable derived class for a given Data Contract. It specifies types that should be recognized by the DataContractSerializer when serializing or deserializing a given type.

一个简单的例子。

[ServiceContract()]
interface ITaskManager
{
    [OperationContract()]
    MyCollection<Task> GetTaskByAssignedName(string name);
}

[DataContract()]
[KnownType(typeof(DerivedTask))]
class Task
{

}

[DataContract()]
class DerivedTask
{

}

当使用多态类型服务合同工作时,KnownTypeAttribute是必需的,因为多态是面向服务的模式之外。

When working with polymorphic types in your service contract, the KnownTypeAttribute is required because polymorphism is outside the paradigm of the service orientation.

应用KnownTypeAttribute属性类型以指示   的DataContractSerializer类型应当承认时   序列化或反序列化类型的实例,以其中   属性被应用。该属性也可以由其它公认的   序列化的理解数据的合同。

Apply the KnownTypeAttribute attribute to a type to indicate to the DataContractSerializer types that should be recognized when serializing or deserializing an instance of the type to which the attribute is applied. This attribute could also be recognized by other serializers that understand data contracts.

这里了解更多详情

这篇关于为什么KnownTypeAttribute需要WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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