无法对派生类进行反序列化.但是,如果它在列表或字典中,它就可以工作.有什么想法吗? [英] Cant deserilize derived class. But it works if it's in a list or a dictionary. Any thoughts?

查看:129
本文介绍了无法对派生类进行反序列化.但是,如果它在列表或字典中,它就可以工作.有什么想法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题,如果我将一个派生类放到一个列表中,但是当它独立存在时就不能序列化.

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);
        }
    }

我有一个基类Entity.还有一个派生类Thing.

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

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

[DataContract]
Class Thing : Entity
{
}

现在,当我尝试序列化实例化为Thing的对象时,没有问题,但是反序列化会导致错误.

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));

该错误显示类似Expected element Entity from ... . Element Thing was found.的字样(不是用英语写的.)

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>));

现在test2的条目具有正确的Thing项目.即使这可能是一种解决方法,但它肯定一定是更简单的方法,而且我认为它一定很容易修复.有什么想法吗?我想念什么?

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?

(当然,当将我的Thing对象反序列化为Thing对象时,它可以工作,但这不是我想要的,当反序列化时,我不会事先知道该类.)

(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.)

推荐答案

在考虑 @ Jaya's之后,我似乎找到了解决方法评论.我修改了serialize方法,以强制将其序列化为基类类型.最初,我认为这不会起作用,但是确实可以!

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();
    }
}

这篇关于无法对派生类进行反序列化.但是,如果它在列表或字典中,它就可以工作.有什么想法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆