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.ToSt ring()); //Causes an
InvalidArgument Exception
} 4 2187
Aaron,
The problem is with your Serialization surrogate. When you assign the
data, you are storing an instance of the color of the Pen. When returning,
you are taking that instance (a Color structure) and trying to cast it to a
pen. What you should do is create a new Pen and set the color, then return
that.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Aaron Clamage" <ac******@nospam.cs.umd.edu> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... 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.ToSt ring()); //Causes an InvalidArgument Exception }
Sorry, that was a typo. But, it is not the problem. Even if the following
line is in the SetObjectData method the problem still occurs. In fact, it
occurs for any non-static reference to a static member.
return new Pen(Color.Black);
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:#v**************@TK2MSFTNGP12.phx.gbl... Aaron,
The problem is with your Serialization surrogate. When you assign the data, you are storing an instance of the color of the Pen. When
returning, you are taking that instance (a Color structure) and trying to cast it to
a pen. What you should do is create a new Pen and set the color, then
return that.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Aaron Clamage" <ac******@nospam.cs.umd.edu> wrote in message news:%2****************@tk2msftngp13.phx.gbl... 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.ToSt ring()); //Causes
an InvalidArgument Exception }
Aaron,
Can you post the definition of D? I would ignore it, but your Child
property returns the first child which is set in the constructor.
Also, if D derives from A, then I can't see how this doesn't result in
an infinite loop (A creates an instance of D, which int turn, creates an
instance of A, etc, etc).
If D doesn't derive from A, then this is an error, because getting the
property A will result in an invalid cast exception.
Could you please post the complete source which compiles?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Aaron Clamage" <ac******@nospam.cs.umd.edu> wrote in message
news:eh**************@TK2MSFTNGP11.phx.gbl... Sorry, that was a typo. But, it is not the problem. Even if the
following line is in the SetObjectData method the problem still occurs. In fact, it occurs for any non-static reference to a static member.
return new Pen(Color.Black);
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:#v**************@TK2MSFTNGP12.phx.gbl... Aaron,
The problem is with your Serialization surrogate. When you assign
the data, you are storing an instance of the color of the Pen. When returning, you are taking that instance (a Color structure) and trying to cast it
to a pen. What you should do is create a new Pen and set the color, then return that.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Aaron Clamage" <ac******@nospam.cs.umd.edu> wrote in message news:%2****************@tk2msftngp13.phx.gbl... 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.ToSt ring()); //Causes an InvalidArgument Exception }
I apologize. I had been trying different things. But, you can ignore the
line referencing D. Here is the correct code sample illustrating the bug
(cut directly from the compiling project).
[Serializable]
public class A {
private ArrayList children;
private static Pen DPEN = Pens.Black;
private Pen pen = DPEN;
public A() {
children = new ArrayList();
}
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 new Pen((Color)info.GetValue("pencolor", typeof(Color)));
}
}
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.ToSt ring()); // Causes an
InvalidArgument exception
}
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eX**************@TK2MSFTNGP12.phx.gbl... Aaron,
Can you post the definition of D? I would ignore it, but your Child property returns the first child which is set in the constructor.
Also, if D derives from A, then I can't see how this doesn't result in an infinite loop (A creates an instance of D, which int turn, creates an instance of A, etc, etc).
If D doesn't derive from A, then this is an error, because getting the property A will result in an invalid cast exception.
Could you please post the complete source which compiles?
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Aaron Clamage" <ac******@nospam.cs.umd.edu> wrote in message news:eh**************@TK2MSFTNGP11.phx.gbl... Sorry, that was a typo. But, it is not the problem. Even if the following line is in the SetObjectData method the problem still occurs. In fact,
it occurs for any non-static reference to a static member.
return new Pen(Color.Black);
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:#v**************@TK2MSFTNGP12.phx.gbl... Aaron,
The problem is with your Serialization surrogate. When you assign the data, you are storing an instance of the color of the Pen. When returning, you are taking that instance (a Color structure) and trying to cast it to a pen. What you should do is create a new Pen and set the color, then return that.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Aaron Clamage" <ac******@nospam.cs.umd.edu> wrote in message news:%2****************@tk2msftngp13.phx.gbl... > 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.ToSt ring());
//Causes an > InvalidArgument Exception > } > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ben |
last post by:
Hi, there.
Recently I was working on a problem where we want to save generic
closures in a data structure (a vector). The closure should work for
any data type and any method with pre-defined...
|
by: andrewcw |
last post by:
There is an error in XML document (1, 2).
I used XML spy to create the XML and XSD. When I asked to
have the XML validated it said it was OK.
I used the .net SDK to generate the class. I have...
|
by: Aaron Clamage |
last post by:
Hi,
I'm not sure that if this is the right forum, but any help would be greatly
appreciated.
I am porting some java serialization code to c# and I can't figure out the
correct way to do it. ...
|
by: Uttam |
last post by:
Hello,
We are at a very crucial decision making stage to select between .Net
and Java. Our requirement is to download a class at runtime on the
client computer and execute it using remoting or...
|
by: Alexander |
last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without
SP1.
An i get this Error:
System.Runtime.Serialization.SerializationException:
Possible Version mismatch. Type...
|
by: mijalko |
last post by:
Hi,
I have inherited my class from System.Drawing.Printing.PrintDocument
and I wish to serialize this object using XmlSerializer. And I get
exception "There was an error reflecting type ...". If I...
|
by: Nikola Skoric |
last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK
until I tried to desirialize a object. There the program died abruptly
dumping this:
System.ArgumentOutOfRangeException:...
|
by: bharathreddy |
last post by:
Before going to that i want to say few thing on serialization :
Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
|
by: kikisan |
last post by:
I am developing a windows service which utilizes the following classes:
interface IPersistable;
abstract class PersistableObject : IPersistable;
|
by: mkvenkit.vc |
last post by:
Hello,
I hope this is the right place to post a question on Boost. If not,
please let me know where I can post this message and I will do so.
I am having a strange problem with std::string as...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |