C# 反序列化泛型列表将返回null

C# 反序列化泛型列表将返回null,c#,serialization,binary-serialization,C#,Serialization,Binary Serialization,我正在反序列化一个对象,如下所示: public class myClass : ISerializable { public List<OType> value; public myClass(SerializationInfo info, StreamingContext context) { this.value = (List<OType>)info.GetValue("value", typeof(List<OType>));

我正在反序列化一个对象,如下所示:

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>));
  }
}
公共类myClass:ISerializable
{
公共列表值;
公共myClass(SerializationInfo信息、StreamingContext上下文)
{
this.value=(List)info.GetValue(“value”,typeof(List));
}
void ISerializable.GetObjectData(SerializationInfo信息,StreamingContext上下文)
{
信息添加值(“值”,值,类型(列表));
}
}
列表中的对象确实具有Serializable属性。序列化时,不会抛出任何错误,列表也不会为空,但反序列化时,我的所有列表都为空,我不知道为什么


我将此标记为CQ的回答。我能够生成一个小的一次性测试应用程序,它可以正确地对我试图使用的对象进行序列化/反序列化,但我仍然无法在我的生产代码中使用它,但我怀疑我缺少了一些小东西。

好的,列表一开始总是空的,您是否通过
myClass.value=new list()设置它?您是否已将序列化数据保存为二进制和xml格式,以便验证数据是否确实已保存

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

编辑:下面的案例似乎序列化和反序列化对我来说很好,我发布这个帖子是因为我误解了整个问题

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

    [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]);
[可序列化]
公共类OType
{
公共标识符{get;set;}
公共字符串SomeData{get;set;}
公共重写字符串ToString()
{
返回string.Format(“{0}:{1}”,SomeIdentifier,SomeData);
}
}
[可序列化]
公共类MyClass:ISerializable
{
公共列表值;
公共MyClass(){}
公共MyClass(SerializationInfo信息、StreamingContext上下文)
{
this.Value=(List)info.GetValue(“Value”,typeof(List));
}
void ISerializable.GetObjectData(SerializationInfo信息,StreamingContext上下文)
{
信息添加值(“值”,值,类型(列表));
}
}
...
var x=新的MyClass();
x、 Value=new-OType[]{new-OType{SomeIdentifier=1,SomeData=“Hello”},new-OType{SomeIdentifier=2,SomeData=“World”}.ToList();
var xSerialized=serialize(x);
WriteLine(“序列化对象为{0}字节”,xSerialized.Length);
var xDeserialized=反序列化(xSerialized);
WriteLine(“{0}{1}”,xDeserialized.Value[0],xDeserialized.Value[1]);
忘记了输出

序列化对象为754字节

1:你好2:世界


当你说你的列表是空的时候,你的意思是列表本身是空的,还是它充满了空的条目?如果是后者,则这是一个已知的.Net问题:请参阅关于同一问题的


基本上,
List
s仅在反序列化时初始化;它们包含的对象仅在对象图被反序列化之后才被反序列化。解决此问题的一种方法是将需要它们的任何代码放入
OnDeserialized
方法中,或者放入带有
[OnDeserializedAttribute]
的方法中。请参阅。

我需要实现iserializable,因为我正在序列化一些位图(未显示),如果不出现gdi错误,您无法以简单的方式直接序列化位图。就像我说的,这个列表从来都不是空的。另外:我还没有将数据序列化为xml格式。我会调查的。也许OType不可序列化?我有一个类似的问题: