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

前端 未结 7 1567
慢半拍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

    The exception that is being reported is for VDB_Sync.Model.Konstant. This means that somewhere further up the chain, this class is being pulled into another class and that class is the one being serialized.

    The issue is that depending on how Konstant is embedded in this class (for example, if it is in a collection or a generic list), the DataContractSerializer may not be prepared for its appearance during deserialization.

    To resolve this, you need to apply the known-type attribute to the class that contains Konstant. Based on your serialization code, I suspect that this is VDB_SessionController.

    So, try decorating this class with the KnownType attribute:

    [KnownType(typeof(VDB_Sync.Model.Konstant)]
    public class VDB_SessionController
    

提交回复
热议问题