473,729 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Reflecti on;
using System.Runtime. Serialization;
using System.Runtime. Serialization.F ormatters;
using System.Runtime. Serialization.F ormatters.Binar y;
using System.Runtime. Serialization.F ormatters.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.Begi n);
f.Deserialize(m s);
}
}
}

[Serializable]
public sealed class myclass
{}

[Serializable]
public class Base
{
public MyStruct myStruct_m;
}

[Serializable]
public class Child : Base//, ISerializable {
public Child(){}

public Child(Serializa tionInfo si, StreamingContex t sc)
{
myStruct_m = (MyStruct) si.GetValue("fo o", typeof(MyStruct ));
}

public void GetObjectData(S erializationInf o si, StreamingContex t sc)
{
si.AddValue("fo o", myStruct_m);
}
}

[Serializable]
public struct MyStruct
{
public object someValue;
}
Nov 16 '05 #1
2 2178
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.goo gle.com...
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.Reflecti on;
using System.Runtime. Serialization;
using System.Runtime. Serialization.F ormatters;
using System.Runtime. Serialization.F ormatters.Binar y;
using System.Runtime. Serialization.F ormatters.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.Begi n);
f.Deserialize(m s);
}
}
}

[Serializable]
public sealed class myclass
{}

[Serializable]
public class Base
{
public MyStruct myStruct_m;
}

[Serializable]
public class Child : Base//, ISerializable {
public Child(){}

public Child(Serializa tionInfo si, StreamingContex t sc)
{
myStruct_m = (MyStruct) si.GetValue("fo o", typeof(MyStruct ));
}

public void GetObjectData(S erializationInf o si, StreamingContex t sc)
{
si.AddValue("fo o", 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 SerializableAtt ribute; 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*******@scie ntiae.com> wrote in message news:<#k******* *******@TK2MSFT NGP10.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.goo gle.com...
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.Reflecti on;
using System.Runtime. Serialization;
using System.Runtime. Serialization.F ormatters;
using System.Runtime. Serialization.F ormatters.Binar y;
using System.Runtime. Serialization.F ormatters.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.Begi n);
f.Deserialize(m s);
}
}
}

[Serializable]
public sealed class myclass
{}

[Serializable]
public class Base
{
public MyStruct myStruct_m;
}

[Serializable]
public class Child : Base//, ISerializable {
public Child(){}

public Child(Serializa tionInfo si, StreamingContex t sc)
{
myStruct_m = (MyStruct) si.GetValue("fo o", typeof(MyStruct ));
}

public void GetObjectData(S erializationInf o si, StreamingContex t sc)
{
si.AddValue("fo o", 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
2421
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 either the list.extend method or even by having the subclass create a whole new list in the variable. The following example illustrates the situation.
4
353
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. Actually, the class I'm serializing is two steps down in the inheritance ladder. It's got a parent class which also has a parent class :( All those classes in the hierarchy are Xml Serializable, and I'd think that it should be obvious that all...
7
2129
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
1649
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 still there. Maybe y'all have some ideas? Background ========== The basic idea behind templates, of course, is that much code is independent
2
2514
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 pointer or reference to a derived type of the base class's return type. In .NET the overridden virtual function is similar, but an actual parameter of the function can be a derived reference from the base class's reference also. This dichotomy...
0
3938
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
8
1675
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 function (particularly, subclass file which is returned from open). I would add some extra functionality and keep the base functions, too. But I am stuck. E.g.
15
3532
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 you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
13
2292
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 problem is that classes in several modules share a common base class which needs to implement a factory method to return instances of these same classes.
0
8917
marktang
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...
0
8761
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,...
0
9426
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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,...
0
9142
tracyyun
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...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
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...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.