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(ne w 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 : ISerializationS urrogate {
public void GetObjectData(O bject obj, SerializationIn fo info,
StreamingContex t context) {
Pen pen = (Pen)obj;
info.AddValue(" pencolor", pen.PenType == PenType.SolidCo lor ? pen.Color :
Color.Empty);
}
public Object SetObjectData(O bject obj, SerializationIn fo info,
StreamingContex t context, ISurrogateSelec tor selector) {
return (Pen)info.GetVa lue("pencolor", typeof(Pen));
}
}
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
A a = new A();
A an = new A();
a.AddChild(an);
BinaryFormatter bFormatter = new BinaryFormatter ();
SurrogateSelect or s = new SurrogateSelect or();
s.AddSurrogate( typeof(Pen), new
StreamingContex t(StreamingCont extStates.All), new PenSurrogate()) ;
bFormatter.Surr ogateSelector = s;
MemoryStream stream = new MemoryStream();
bFormatter.Seri alize(stream, a);
stream.Seek(0, SeekOrigin.Begi n);
A copy = (A)bFormatter.D eserialize(stre am);
System.Console. WriteLine(copy. Pen.Color.ToStr ing());
System.Console. WriteLine(copy. Child.Pen.Color .ToString()); //Causes an
InvalidArgument Exception
} 4 2211
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.co m
"Aaron Clamage" <ac******@nospa m.cs.umd.edu> wrote in message
news:%2******** ********@tk2msf tngp13.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(ne w 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 : ISerializationS urrogate { public void GetObjectData(O bject obj, SerializationIn fo info, StreamingContex t context) { Pen pen = (Pen)obj;
info.AddValue(" pencolor", pen.PenType == PenType.SolidCo lor ? pen.Color
: Color.Empty); }
public Object SetObjectData(O bject obj, SerializationIn fo info, StreamingContex t context, ISurrogateSelec tor selector) { return (Pen)info.GetVa lue("pencolor", typeof(Pen)); } }
public Form1() { // // Required for Windows Form Designer support // InitializeCompo nent();
A a = new A(); A an = new A(); a.AddChild(an);
BinaryFormatter bFormatter = new BinaryFormatter (); SurrogateSelect or s = new SurrogateSelect or(); s.AddSurrogate( typeof(Pen), new StreamingContex t(StreamingCont extStates.All), new PenSurrogate()) ; bFormatter.Surr ogateSelector = s; MemoryStream stream = new MemoryStream(); bFormatter.Seri alize(stream, a); stream.Seek(0, SeekOrigin.Begi n); A copy = (A)bFormatter.D eserialize(stre am);
System.Console. WriteLine(copy. Pen.Color.ToStr ing()); System.Console. WriteLine(copy. Child.Pen.Color .ToString()); //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.c om> wrote in
message news:#v******** ******@TK2MSFTN GP12.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.co m
"Aaron Clamage" <ac******@nospa m.cs.umd.edu> wrote in message news:%2******** ********@tk2msf tngp13.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(ne w 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 : ISerializationS urrogate { public void GetObjectData(O bject obj, SerializationIn fo info, StreamingContex t context) { Pen pen = (Pen)obj;
info.AddValue(" pencolor", pen.PenType == PenType.SolidCo lor ?
pen.Color : Color.Empty); }
public Object SetObjectData(O bject obj, SerializationIn fo info, StreamingContex t context, ISurrogateSelec tor selector) { return (Pen)info.GetVa lue("pencolor", typeof(Pen)); } }
public Form1() { // // Required for Windows Form Designer support // InitializeCompo nent();
A a = new A(); A an = new A(); a.AddChild(an);
BinaryFormatter bFormatter = new BinaryFormatter (); SurrogateSelect or s = new SurrogateSelect or(); s.AddSurrogate( typeof(Pen), new StreamingContex t(StreamingCont extStates.All), new PenSurrogate()) ; bFormatter.Surr ogateSelector = s; MemoryStream stream = new MemoryStream(); bFormatter.Seri alize(stream, a); stream.Seek(0, SeekOrigin.Begi n); A copy = (A)bFormatter.D eserialize(stre am);
System.Console. WriteLine(copy. Pen.Color.ToStr ing()); System.Console. WriteLine(copy. Child.Pen.Color .ToString()); //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.co m
"Aaron Clamage" <ac******@nospa m.cs.umd.edu> wrote in message
news:eh******** ******@TK2MSFTN GP11.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.c om> wrote
in message news:#v******** ******@TK2MSFTN GP12.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.co m
"Aaron Clamage" <ac******@nospa m.cs.umd.edu> wrote in message news:%2******** ********@tk2msf tngp13.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(ne w 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 : ISerializationS urrogate { public void GetObjectData(O bject obj, SerializationIn fo info, StreamingContex t context) { Pen pen = (Pen)obj;
info.AddValue(" pencolor", pen.PenType == PenType.SolidCo lor ? pen.Color : Color.Empty); }
public Object SetObjectData(O bject obj, SerializationIn fo info, StreamingContex t context, ISurrogateSelec tor selector) { return (Pen)info.GetVa lue("pencolor", typeof(Pen)); } }
public Form1() { // // Required for Windows Form Designer support // InitializeCompo nent();
A a = new A(); A an = new A(); a.AddChild(an);
BinaryFormatter bFormatter = new BinaryFormatter (); SurrogateSelect or s = new SurrogateSelect or(); s.AddSurrogate( typeof(Pen), new StreamingContex t(StreamingCont extStates.All), new PenSurrogate()) ; bFormatter.Surr ogateSelector = s; MemoryStream stream = new MemoryStream(); bFormatter.Seri alize(stream, a); stream.Seek(0, SeekOrigin.Begi n); A copy = (A)bFormatter.D eserialize(stre am);
System.Console. WriteLine(copy. Pen.Color.ToStr ing()); System.Console. WriteLine(copy. Child.Pen.Color .ToString()); //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 : ISerializationS urrogate {
public void GetObjectData(O bject obj, SerializationIn fo info,
StreamingContex t context) {
Pen pen = (Pen)obj;
info.AddValue(" pencolor", pen.PenType == PenType.SolidCo lor ? pen.Color :
Color.Empty);
}
public Object SetObjectData(O bject obj, SerializationIn fo info,
StreamingContex t context, ISurrogateSelec tor selector) {
return new Pen((Color)info .GetValue("penc olor", typeof(Color))) ;
}
}
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
A a = new A();
A an = new A();
a.AddChild(an);
BinaryFormatter bFormatter = new BinaryFormatter ();
SurrogateSelect or s = new SurrogateSelect or();
s.AddSurrogate( typeof(Pen), new
StreamingContex t(StreamingCont extStates.All), new PenSurrogate()) ;
bFormatter.Surr ogateSelector = s;
MemoryStream stream = new MemoryStream();
bFormatter.Seri alize(stream, a);
stream.Seek(0, SeekOrigin.Begi n);
A copy = (A)bFormatter.D eserialize(stre am);
System.Console. WriteLine(copy. Pen.Color.ToStr ing());
System.Console. WriteLine(copy. Child.Pen.Color .ToString()); // Causes an
InvalidArgument exception
}
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:eX******** ******@TK2MSFTN GP12.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.co m
"Aaron Clamage" <ac******@nospa m.cs.umd.edu> wrote in message news:eh******** ******@TK2MSFTN GP11.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.c om> wrote in message news:#v******** ******@TK2MSFTN GP12.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.co m
"Aaron Clamage" <ac******@nospa m.cs.umd.edu> wrote in message news:%2******** ********@tk2msf tngp13.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(ne w 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 : ISerializationS urrogate { > public void GetObjectData(O bject obj, SerializationIn fo info, > StreamingContex t context) { > Pen pen = (Pen)obj; > > info.AddValue(" pencolor", pen.PenType == PenType.SolidCo lor ? pen.Color : > Color.Empty); > } > > public Object SetObjectData(O bject obj, SerializationIn fo info, > StreamingContex t context, ISurrogateSelec tor selector) { > return (Pen)info.GetVa lue("pencolor", typeof(Pen)); > } > } > > public Form1() > { > // > // Required for Windows Form Designer support > // > InitializeCompo nent(); > > A a = new A(); > A an = new A(); > a.AddChild(an); > > BinaryFormatter bFormatter = new BinaryFormatter (); > SurrogateSelect or s = new SurrogateSelect or(); > s.AddSurrogate( typeof(Pen), new > StreamingContex t(StreamingCont extStates.All), new PenSurrogate()) ; > bFormatter.Surr ogateSelector = s; > MemoryStream stream = new MemoryStream(); > bFormatter.Seri alize(stream, a); > stream.Seek(0, SeekOrigin.Begi n); > A copy = (A)bFormatter.D eserialize(stre am); > > System.Console. WriteLine(copy. Pen.Color.ToStr ing()); > System.Console. WriteLine(copy. Child.Pen.Color .ToString());
//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 signature.
When developing this lib, I figured that the
pointer-to-member-function, although seemingly an attractive solution,
does not work well for us.
|
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 done
this before but this time I have no idea why I am getting
the error. Any ideas ?? THANKS !
<qcsttatus>
<manual sourcerootfolder="" manualname="">
|
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. It seems that either I can use default serialization
or implement ISerializable. Is there any way to do both (e.g. extend the
default serialization). In other words, I want to be able to implement my
custom serialization code but call the...
|
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 rmi.
Just to keep my question short I am posting trimmed version of my
code.
//file: Serializable.cs
|
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 System.Collections.Comparer has 1 members,
number of members deserialized is 0.
at
System.Runtime.Serialization.Formatters.Binary.ReadObjectInfo.GetMemberTypes(String
| |
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 look at
innerException it says: "Cannot serialize member
'System.ComponentModel.Component.Site' of type
'System.ComponentModel.ISite'.
OK it is problem to serialize all data so I'll implement my
Serialization. I implemented ISerializable...
|
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: Value -8590321990885400808 is
outside the valid range .
Parameter name: ticks
at System.DateTime..ctor (Int64 ticks)
at
System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadPrimitiv
|
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 object and transport it over the Internet using HTTP between a client and a server. On the other end, deserialization reconstructs the object from the stream.
XML serialization serializes only the public fields and property values of an object...
|
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 I am trying to read
from a binary archive using Boost serialization. I am new to this, and
it is possible that I have not understood the usage. In the code
below, the string "faultblock" seems to be causing the problem. The
code crashes in the ia...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |