Note that there are some explanatory texts on larger screens.

plurals
  1. POSerialization.InvalidDataContractException was unhandled
    primarykey
    data
    text
    <pre><code>public class SerializablionHelper { public SerializablionHelper() { this.Entities = new Collection&lt;Base&gt;(); } public ICollection&lt;Base&gt; Entities { get; set; } } // [DataContract] // [KnownType(typeof(A))] // [KnownType(typeof(B))] public abstract class Base { } [DataContract] public class A : Base { public int IntTest { get; set; } [DataMember] public string StringTest { get; set; } } [DataContract] public class B : Base { [DataMember] public int IntTest { get; set; } [DataMember] public string StringTest { get; set; } } public static void Main(string[] args) { var objectA = new A { IntTest = 5, StringTest = "TestA" }; var objectB = new B { IntTest = 25, StringTest = "TestB" }; var serList = new SerializablionHelper(); serList.Entities.Add(objectA); serList.Entities.Add(objectB); var dcSerializer = new DataContractSerializer(typeof(SerializablionHelper)); var xmlWriterSetting = new XmlWriterSettings() { Indent = true }; using (var xmlWriter = XmlWriter.Create("C:\\test.xml", xmlWriterSetting)) { dcSerializer.WriteObject(xmlWriter, serList); } } </code></pre> <p>When I run this code I get this exception.</p> <p>Type 'XMLSerializer.Program+A' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute. Consider marking the base type 'XMLSerializer.Program+Base' with DataContractAttribute or SerializableAttribute, or removing them from the derived type.</p> <p>When I recommitted this code:</p> <pre><code>// [DataContract] // [KnownType(typeof(A))] // [KnownType(typeof(B))] </code></pre> <p>The code will working.</p> <p>The problem I cannot make the base class as DataContract. Is there is any way to make this code working without Adding the DataContract to the base class or I have to take another serialization strategy?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload