Cant deserilize derived class. But it works if it's in a list or a dictionary. Any thoughts?

三世轮回 提交于 2019-12-12 04:15:46

问题


I got a weird problem where I can serialize a derived class if I put it in a List but not when it stands on it own.

I am using these methods to serialize and deserialize (found them here):

    public static string Serialize(T obj)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        using (StreamReader reader = new StreamReader(memoryStream))
        {
            DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
            serializer.WriteObject(memoryStream, obj);
            memoryStream.Position = 0;
            return reader.ReadToEnd();
        }
    }

    public static T Deserialize(string xml, Type toType)
    {
        using (Stream stream = new MemoryStream())
        {
            byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
            stream.Write(data, 0, data.Length);
            stream.Position = 0;
            DataContractSerializer deserializer = new DataContractSerializer(toType);
            return (T)deserializer.ReadObject(stream);
        }
    }

I have a base class Entity. And a derived class Thing.

[DataContract]
[KnownType(typeof(Thing))]
Class Entity
{
}

[DataContract]
Class Thing : Entity
{
}

Now, when I try to serialize an object instantiated as Thing there is no problem but the deserialization gives an error.

            Thing e = new Thing();
            string xmlstring = XML<Entity>.Serialize(e); 

            Entity testE = XML<Entity>.Deserialize(xmlstring, typeof(Entity));

The error says something like Expected element Entity from ... . Element Thing was found. (Was not written in english.)

But the weird thing is that it works if I put the object into a list and serialize-deserialize the list.

            Thing e = new Thing();
            List<Entity> test = new List<Entity>();
            test.Add(e);

            var xmlstring = XML<List<Entity>>.Serialize(test);

            List<Entity> test2 = XML<List<Entity>>.Deserialize(xmlstring, typeof(List<Entity>));

Now test2 has an entry with the a correct Thing-item. Even if this could be a workaround it surely must be an easier way and it must be something that would be easy to fix I think? Any thoughts? What did I miss?

(And of course it works when deserializing my Thingobject as a Thing-object but that's not what I want, when deserializing I will not know the class on beforehand.)


回答1:


Seems that I have found a workaround myself after thinking about @Jaya's comment. I modified the serialize method to force it to serialize to the base class type. Originally I did not think this would work, but it did!

public static string Serialize(T obj, Type toType)
{
    using (MemoryStream memoryStream = new MemoryStream())
    using (StreamReader reader = new StreamReader(memoryStream))
    {
        DataContractSerializer serializer = new DataContractSerializer(toType);
        serializer.WriteObject(memoryStream, obj);
        memoryStream.Position = 0;
        return reader.ReadToEnd();
    }
}


来源:https://stackoverflow.com/questions/45443167/cant-deserilize-derived-class-but-it-works-if-its-in-a-list-or-a-dictionary-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!