“Type not expected”, using DataContractSerializer - but it's just a simple class, no funny stuff?

前端 未结 7 1561
慢半拍i
慢半拍i 2020-11-30 11:10

I\'m refactoring my XML-serialization, and figured I\'d try the DataContractSerializer. Everything runs smoothly, until it needs to serialize this class:

         


        
7条回答
  •  暖寄归人
    2020-11-30 11:35

    It is like as @Leon suggested but with the fix from @Bryan, the 'KnownTypeAttribute' should be on the base class, so it should be like this:

    [DataContract(Name="DataFelt")]
    [KnownType(typeof(somenamespace.Konstant))]
    public class DataFelt
    

    and in the Subclass:

    [DataContract(Name="Konstant")]
    public class Konstant : DataFelt
    

提交回复
热议问题