反序列化泛型列表返回 null [英] deserializing a generic list returns null

查看:41
本文介绍了反序列化泛型列表返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在反序列化一个对象:

I'm de/serializing an object like so:

public class myClass : ISerializable
{
  public List<OType> value;

  public myClass(SerializationInfo info, StreamingContext context)
  {
    this.value = (List<OType>)info.GetValue("value", typeof(List<OType>));
  }

  void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  {
    info.AddValue("value", value, typeof(List<OType>));
  }
}

列表中的对象确实具有 Serializable 属性.序列化时,不会抛出任何错误,并且列表从不 为空,但是在反序列化时,我的所有列表都为空,我不知道为什么.

The object that is in the list does have the Serializable attribute. When serializing, no errors are thrown and the list is never empty, but when deserializing all of my lists are null and I'm not sure why.

我将此标记为 CQ 的回答.我能够生成一个小的一次性测试应用程序,它可以正确地序列化/反序列化我正在尝试使用的对象,但我似乎仍然无法让它在我的生产代码中工作,但我怀疑这是我的一些小东西不见了.

I'm marking this as answered by CQ. I was able to produce a small one off test app that does properly serialize/deserialize with the objects I'm trying to use but I still can't seem to get it to work in my production code but I suspect it's something small that I'm missing.

推荐答案

好吧,列表一开始总是空的,你是通过 myClass.value = new List<...>(); 设置的吗? ?您是否还以二进制和 xml 格式保存了序列化数据,以便验证数据是否确实被保存了?

Well the list is always empty to begin with, are you setting it via myClass.value = new List<...>(); ? Also have you saved the serialized data in both binary and xml formats so you can verify data is actually being saved?

也请注意,如果您使用的是 2.0+,如果您不需要控制绝对序列化,则不必实现 ISerializable,您可以将值更改为公共属性,它会自行序列化.

Just a note as well, if you are using 2.0+ you don't have to implement ISerializable if you don't need to control the absolute serialization, you can change value to a public property and it will serialize on it's own.

以下案例似乎对我来说序列化和反序列化很好,我发布这个以防我误解了整个问题.

The following case seems to serialize and deserialize fine for me, I am posting this incase I am misunderstanding the question as a whole.

忽略讨厌的测试代码,希望这会有所帮助.

Ignoring the nasty test code, hopefully this helps a little.

    [Serializable]
    public class OType
    {
        public int SomeIdentifier { get; set; }
        public string SomeData { get; set; }

        public override string ToString()
        {
            return string.Format("{0}: {1}", SomeIdentifier, SomeData);
        }
    }

    [Serializable]
    public class MyClass : ISerializable
    {
        public List<OType> Value;

        public MyClass() {  }

        public MyClass(SerializationInfo info, StreamingContext context)
        {
            this.Value = (List<OType>)info.GetValue("value", typeof(List<OType>));
        }

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("value", Value, typeof(List<OType>));
        }
    }

...

        var x = new MyClass();

        x.Value = new OType[] { new OType { SomeIdentifier = 1, SomeData = "Hello" }, new OType { SomeIdentifier = 2, SomeData = "World" } }.ToList();

        var xSerialized = serialize(x);

        Console.WriteLine("Serialized object is {0}bytes", xSerialized.Length);

        var xDeserialized = deserialize<MyClass>(xSerialized);

        Console.WriteLine("{0} {1}", xDeserialized.Value[0], xDeserialized.Value[1]);

忘记输出了..

序列化对象为 754 字节

Serialized object is 754bytes

1:你好 2:世界

这篇关于反序列化泛型列表返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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