A
Andrew G. J. Fung
Can anyone please explain to me why the code below throws an exception?
It seems to occur when deserializing an instance of a subclass, whose
base class holds a struct, where said struct holds an instance to a
reference type (but not System.String).
The commented out code are lines that when enabled (and any conflicting
lines disabled) stop the exception from being thrown.
Thanks kindly,
Andrew.
---
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
public class test
{
public static void Main()
{
//Base top = new Base();
Child top = new Child();
top .myStruct_m = new MyStruct();
top.myStruct_m.someValue = new myclass();
//top.myStruct_m.someValue = "foobar";
BinaryFormatter f = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
f.Serialize(ms, top);
ms.Seek(0, SeekOrigin.Begin);
f.Deserialize(ms);
}
}
}
[Serializable]
public sealed class myclass
{}
[Serializable]
public class Base
{
public MyStruct myStruct_m;
}
[Serializable]
public class Child : Base//, ISerializable {
public Child(){}
public Child(SerializationInfo si, StreamingContext sc)
{
myStruct_m = (MyStruct) si.GetValue("foo", typeof(MyStruct));
}
public void GetObjectData(SerializationInfo si, StreamingContext sc)
{
si.AddValue("foo", myStruct_m);
}
}
[Serializable]
public struct MyStruct
{
public object someValue;
}
It seems to occur when deserializing an instance of a subclass, whose
base class holds a struct, where said struct holds an instance to a
reference type (but not System.String).
The commented out code are lines that when enabled (and any conflicting
lines disabled) stop the exception from being thrown.
Thanks kindly,
Andrew.
---
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
public class test
{
public static void Main()
{
//Base top = new Base();
Child top = new Child();
top .myStruct_m = new MyStruct();
top.myStruct_m.someValue = new myclass();
//top.myStruct_m.someValue = "foobar";
BinaryFormatter f = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
f.Serialize(ms, top);
ms.Seek(0, SeekOrigin.Begin);
f.Deserialize(ms);
}
}
}
[Serializable]
public sealed class myclass
{}
[Serializable]
public class Base
{
public MyStruct myStruct_m;
}
[Serializable]
public class Child : Base//, ISerializable {
public Child(){}
public Child(SerializationInfo si, StreamingContext sc)
{
myStruct_m = (MyStruct) si.GetValue("foo", typeof(MyStruct));
}
public void GetObjectData(SerializationInfo si, StreamingContext sc)
{
si.AddValue("foo", myStruct_m);
}
}
[Serializable]
public struct MyStruct
{
public object someValue;
}