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

前端 未结 7 1543
慢半拍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:10

    You can also combine [KnownType] and reflection to make your code more resistant to future changes.

    [DataContract]
    [KnownType("GetKnownPersonTypes")]
    internal class Person
    {
        private static IEnumerable<Type> _personTypes;
    
        private static IEnumerable<Type> GetKnownTypes()
        {
            if (_personTypes == null)
                _personTypes = Assembly.GetExecutingAssembly()
                                        .GetTypes()
                                        .Where(t => typeof (Person).IsAssignableFrom(t))
                                        .ToList();
            return _personTypes;
        }
    }
    

    Now a DataContractSerializer / DataContractJsonSerializer / XmlSerializer configured to work with Person, will also work with any type derived from Person (as long as it's declared within the same assembly).

    0 讨论(0)
  • 2020-11-30 11:16

    The problem for me was that I was returning the Interface (IIndividual) from my WebAPI controller. When I changed that return type to the Class (Individual) type, this error went away.

    Not working:

        [HttpGet]
        [Route("api/v1/Individual/Get/{id}")]
        public IIndividual Get([FromUri]int id)
        {
            return _individualService.Get(id);
        }
    

    Working:

        [HttpGet]
        [Route("api/v1/Individual/Get/{id}")]
        public Individual Get([FromUri]int id)
        {
            IIndividual individual = _individualService.Get(id);
            return individual as Individual;
        }
    
    0 讨论(0)
  • 2020-11-30 11:27

    Change this:

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

    to this:

    [DataContract(Name="Konstant")]
    [KnownTypes(typeof(somenamespace.DataFelt))]
    public class Konstant : DataFelt
    
    0 讨论(0)
  • 2020-11-30 11:29

    use KnownTypeAttribute for resolving DataFelt class. see: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.knowntypeattribute.aspx

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题