问题描述
我学习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类型应当承认时 序列化或反序列化类型的实例,以其中 属性被应用。该属性也可以由其它公认的 序列化的理解数据的合同。
看这里了解更多详情
这篇关于为什么KnownTypeAttribute需要WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!