我已经尝试了两天来解决以下问题。
我有一个抽象类
[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))属性,但这无济于事。我将不胜感激任何建议。提前谢谢