473,326 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Problem deserializing a subclass whose base class has a struct that has a reference type

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;
}
Nov 16 '05 #1
2 2152
The exception you are getting is because Child is not serializable. you
need to take out the comments on ISerializable from the Child class.
"Andrew G. J. Fung" <An*********@k-a.dhs.org> wrote in message
news:ce**************************@posting.google.c om...
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;
}

Nov 16 '05 #2
Hi, Ben,

Sorry, the code I posted incorrectly had the brace on the same line as
the class declaration for the child class; thus it won't compile "as
is", when I intended it to.

However, I believe the child class *is* serializable, because it is
marked with the SerializableAttribute; implementing ISerializable is
not necessary for a class to be considered serializable. The
interesting thing there is that implementing my own custom
serialization scheme by implementing the ISerializable interface
causes the problem to go away, while achieving the desired result
(serialize and deserialize).

Similarly, if we store a System.String in the struct, there is no
exception. Or if we serialize/deserialize an instance of the base
class rather than the subclass, even though the subclass doesn't
define any new characteristics.

Thanks!
Andrew.

"Ben Dewey" <be*******@scientiae.com> wrote in message news:<#k**************@TK2MSFTNGP10.phx.gbl>...
The exception you are getting is because Child is not serializable. you
need to take out the comments on ISerializable from the Child class.
"Andrew G. J. Fung" <An*********@k-a.dhs.org> wrote in message
news:ce**************************@posting.google.c om...
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;
}

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
4
by: Angelos Karantzalis | last post by:
Hi guys. I've come across a problem when I tried to serialize a class into xml, only to discover that the parent class's XML Serialization properties weren't included in the output xml. ...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
5
by: Michael Olea | last post by:
Here is a design problem I ran into this am - and I have cleaned the bathroom, scrubbed toilet sink and tub, windexed all glass, mopped the floor, and vacuumed the house - no dice, the problem is...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
8
by: Lou Pecora | last post by:
I've been scanning Python in a Nutshell, but this seems to be either undoable or so subtle that I don't know how to do it. I want to subclass a base class that is returned from a Standard Library...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
13
by: Rafe | last post by:
Hi, I am in a situation where I feel I am being forced to abandon a clean module structure in favor of a large single module. If anyone can save my sanity here I would be forever grateful. My...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.