无法创建抽象类wcf

时间:2018-07-13 08:59:38

标签: c# xml wcf serialization xsd2code

我已经尝试了两天来解决以下问题。

我有一个抽象类

[System.Xml.Serialization.XmlIncludeAttribute(typeof(PhysicalAddress))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(POBoxAddress))]   
[System.SerializableAttribute()]
[System.Runtime.Serialization.DataContractAttribute(Name = "Address", Namespace = "somenamespace")]
public abstract class Address {}

和两个派生类

PostalAddress : Address {}
PhysicalAddress : Address {}

这些都是Example.Designer.cs文件中所有由xsd2code生成的类。 在我的Customer.Designer.cs类中,我有以下

private Address _postalAddress;
public Address PostalAddress {
get{
return this._postalAddress;
}
set
{
   this._postalAddress = value;
 }
 }

当我运行wcf服务并尝试使用PostalAddress创建客户时,出现以下错误

    <Message>Cannot create an abstract class.</Message>
               <StackTrace><![CDATA[at System.Runtime.Serialization.FormatterServices.nativeGetUninitializedObject(RuntimeType type)
   at System.Runtime.Serialization.FormatterServices.GetUninitializedObject(Type type)
   at System.Runtime.Serialization.XmlFormatReaderGenerator.UnsafeGetUninitializedObject(Int32 id)
   at ReadAddressFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[].....

我尝试在抽象的Address类上设置KnownType(typeof(PostalAddress))属性,但这无济于事。我将不胜感激任何建议。提前谢谢

0 个答案:

没有答案