A
Aaron Clamage
Hi,
Could someone please confirm the following?
I think I have found a subtle .NET serialization bug. It occurs when object
has a list of items containing another object of the same type and both
objects have a non-static member reference to some other static object. In
this case, I get an InvalidArgument exception when I try to access the
non-static member of the child class. I've included a code sample to
clarify. This "bug" is causing a major problem for me. So, if anyone could
confirm it or let me know if I'm doing something wrong, I would be very
appreciative.
Thanks,
Aaron
Here is the sample
[Serializable]
public class A {
private ArrayList children;
private static Pen DPEN = Pens.Black;
private Pen pen = DPEN;
public A() {
children = new ArrayList();
children.Add(new D());
}
public void AddChild(A a) {
children.Add(a);
}
public A Child {
get { return (A)children[0]; }
}
public Pen Pen {
get { return pen; }
}
}
public class PenSurrogate : ISerializationSurrogate {
public void GetObjectData(Object obj, SerializationInfo info,
StreamingContext context) {
Pen pen = (Pen)obj;
info.AddValue("pencolor", pen.PenType == PenType.SolidColor ? pen.Color :
Color.Empty);
}
public Object SetObjectData(Object obj, SerializationInfo info,
StreamingContext context, ISurrogateSelector selector) {
return (Pen)info.GetValue("pencolor", typeof(Pen));
}
}
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
A a = new A();
A an = new A();
a.AddChild(an);
BinaryFormatter bFormatter = new BinaryFormatter();
SurrogateSelector s = new SurrogateSelector();
s.AddSurrogate(typeof(Pen), new
StreamingContext(StreamingContextStates.All), new PenSurrogate());
bFormatter.SurrogateSelector = s;
MemoryStream stream = new MemoryStream();
bFormatter.Serialize(stream, a);
stream.Seek(0, SeekOrigin.Begin);
A copy = (A)bFormatter.Deserialize(stream);
System.Console.WriteLine(copy.Pen.Color.ToString());
System.Console.WriteLine(copy.Child.Pen.Color.ToString()); //Causes an
InvalidArgument Exception
}
Could someone please confirm the following?
I think I have found a subtle .NET serialization bug. It occurs when object
has a list of items containing another object of the same type and both
objects have a non-static member reference to some other static object. In
this case, I get an InvalidArgument exception when I try to access the
non-static member of the child class. I've included a code sample to
clarify. This "bug" is causing a major problem for me. So, if anyone could
confirm it or let me know if I'm doing something wrong, I would be very
appreciative.
Thanks,
Aaron
Here is the sample
[Serializable]
public class A {
private ArrayList children;
private static Pen DPEN = Pens.Black;
private Pen pen = DPEN;
public A() {
children = new ArrayList();
children.Add(new D());
}
public void AddChild(A a) {
children.Add(a);
}
public A Child {
get { return (A)children[0]; }
}
public Pen Pen {
get { return pen; }
}
}
public class PenSurrogate : ISerializationSurrogate {
public void GetObjectData(Object obj, SerializationInfo info,
StreamingContext context) {
Pen pen = (Pen)obj;
info.AddValue("pencolor", pen.PenType == PenType.SolidColor ? pen.Color :
Color.Empty);
}
public Object SetObjectData(Object obj, SerializationInfo info,
StreamingContext context, ISurrogateSelector selector) {
return (Pen)info.GetValue("pencolor", typeof(Pen));
}
}
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
A a = new A();
A an = new A();
a.AddChild(an);
BinaryFormatter bFormatter = new BinaryFormatter();
SurrogateSelector s = new SurrogateSelector();
s.AddSurrogate(typeof(Pen), new
StreamingContext(StreamingContextStates.All), new PenSurrogate());
bFormatter.SurrogateSelector = s;
MemoryStream stream = new MemoryStream();
bFormatter.Serialize(stream, a);
stream.Seek(0, SeekOrigin.Begin);
A copy = (A)bFormatter.Deserialize(stream);
System.Console.WriteLine(copy.Pen.Color.ToString());
System.Console.WriteLine(copy.Child.Pen.Color.ToString()); //Causes an
InvalidArgument Exception
}