473,387 Members | 1,485 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,387 software developers and data experts.

Serialization of a custom class

Joe
I have several classes which I need serialized. Here is their class
definitions:

public class TopContainer
{
private LevelTwoType m_levelTwo;
public LevelTwoType LevelTwo
{
get
{
return m_levelTwo;
}
}
....
}

public class LevelTwoType
{
private ArrayList m_levelThree;

public LevelThreeType LevelThree this [int index]
{
get
{
return (LevelThreeType)m_levelThree[index];
}
}
// Plus the following
Add(LevelThreeType)
Count
....
}

there are 2 more classes similar to LevelTwoType class.

I can't get these to serialize. I tried using the ISerializable interface
and implement the GetObjectData method but still no luck.

If I use the ICollection interface it sort of works but not the way I need
it to.
Nov 16 '05 #1
8 5990
Joe,

Is LevelTwoType serializable? It needs to be in order to serialize the
TopContainer class.

If it is, then it looks like there is nothing that dictates that you
need custom serialization. In that case, I would just tag the class with
the Serializable attribute and not implement custom serialization.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I have several classes which I need serialized. Here is their class
definitions:

public class TopContainer
{
private LevelTwoType m_levelTwo;
public LevelTwoType LevelTwo
{
get
{
return m_levelTwo;
}
}
...
}

public class LevelTwoType
{
private ArrayList m_levelThree;

public LevelThreeType LevelThree this [int index]
{
get
{
return (LevelThreeType)m_levelThree[index];
}
}
// Plus the following
Add(LevelThreeType)
Count
...
}

there are 2 more classes similar to LevelTwoType class.

I can't get these to serialize. I tried using the ISerializable interface
and implement the GetObjectData method but still no luck.

If I use the ICollection interface it sort of works but not the way I need
it to.

Nov 16 '05 #2
ISerialisable is definately the way forward if you want to serialise to
binary stream.

From what i remember you need a serialisation contructor (which instantiates
the object from the serialised data).
A GetObjectData method which is used to add (member) data to a serialisation
object

There's another method you need to implement too and serialiable class
attribute needs adding also. I can't check at moment as installing SP, but
look in google for an example of implementing ISerialisable, you wont be far
wrong.

If you get stuck, reply and I'll try to give you more info later (when my
machine's sorted)

HTH
Sam

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I have several classes which I need serialized. Here is their class
definitions:

public class TopContainer
{
private LevelTwoType m_levelTwo;
public LevelTwoType LevelTwo
{
get
{
return m_levelTwo;
}
}
...
}

public class LevelTwoType
{
private ArrayList m_levelThree;

public LevelThreeType LevelThree this [int index]
{
get
{
return (LevelThreeType)m_levelThree[index];
}
}
// Plus the following
Add(LevelThreeType)
Count
...
}

there are 2 more classes similar to LevelTwoType class.

I can't get these to serialize. I tried using the ISerializable interface
and implement the GetObjectData method but still no luck.

If I use the ICollection interface it sort of works but not the way I need
it to.

Nov 16 '05 #3
Joe
I believe it is Serializable. How can I tell for sure? Is there something
that would make it NOT serializable?

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ol**************@TK2MSFTNGP10.phx.gbl...
Joe,

Is LevelTwoType serializable? It needs to be in order to serialize the TopContainer class.

If it is, then it looks like there is nothing that dictates that you
need custom serialization. In that case, I would just tag the class with
the Serializable attribute and not implement custom serialization.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I have several classes which I need serialized. Here is their class
definitions:

public class TopContainer
{
private LevelTwoType m_levelTwo;
public LevelTwoType LevelTwo
{
get
{
return m_levelTwo;
}
}
...
}

public class LevelTwoType
{
private ArrayList m_levelThree;

public LevelThreeType LevelThree this [int index]
{
get
{
return (LevelThreeType)m_levelThree[index];
}
}
// Plus the following
Add(LevelThreeType)
Count
...
}

there are 2 more classes similar to LevelTwoType class.

I can't get these to serialize. I tried using the ISerializable interface and implement the GetObjectData method but still no luck.

If I use the ICollection interface it sort of works but not the way I need it to.


Nov 16 '05 #4
Joe,

If it doesn't have the Serializable attribute on it (LevelTwoType), then
it is not serializable.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I believe it is Serializable. How can I tell for sure? Is there something
that would make it NOT serializable?

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:Ol**************@TK2MSFTNGP10.phx.gbl...
Joe,

Is LevelTwoType serializable? It needs to be in order to serialize

the
TopContainer class.

If it is, then it looks like there is nothing that dictates that you
need custom serialization. In that case, I would just tag the class with
the Serializable attribute and not implement custom serialization.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
>I have several classes which I need serialized. Here is their class
> definitions:
>
> public class TopContainer
> {
> private LevelTwoType m_levelTwo;
> public LevelTwoType LevelTwo
> {
> get
> {
> return m_levelTwo;
> }
> }
> ...
> }
>
> public class LevelTwoType
> {
> private ArrayList m_levelThree;
>
> public LevelThreeType LevelThree this [int index]
> {
> get
> {
> return (LevelThreeType)m_levelThree[index];
> }
> }
> // Plus the following
> Add(LevelThreeType)
> Count
> ...
> }
>
> there are 2 more classes similar to LevelTwoType class.
>
> I can't get these to serialize. I tried using the ISerializable interface > and implement the GetObjectData method but still no luck.
>
> If I use the ICollection interface it sort of works but not the way I need > it to.
>
>



Nov 16 '05 #5
Joe
I have the constructor as for ISerializable as well but still no luck.

Here's some more information:
I have the [Serializable] attribute directly before the class definition and
the class implements ISerializable.

The purpose of this is to return the TopContainer class from a WebMethod.
When I look at the web reference that is created, I see the class names but
TopContainer doesn't contain a LevelTwoType.

TopContainer encapsulates LevelTwoType which encapsulates an arraylist,
which is an Array of LevelThreeType. LevelThreeType encapsulates another
ArrayList which is an Array of LevelFourType. LevelFourType encapsulates a
class which encapsulates an ArrayList (this class does serialize because I
use it in other places and it works fine )

All classes have the [Serializable] attribute.

I hope this isn't too confusing.

Thanks again.

"Sam Martin" <sa*********@yahoo.co.uk> wrote in message
news:#y**************@TK2MSFTNGP14.phx.gbl...
ISerialisable is definately the way forward if you want to serialise to
binary stream.

From what i remember you need a serialisation contructor (which instantiates the object from the serialised data).
A GetObjectData method which is used to add (member) data to a serialisation object

There's another method you need to implement too and serialiable class
attribute needs adding also. I can't check at moment as installing SP, but look in google for an example of implementing ISerialisable, you wont be far wrong.

If you get stuck, reply and I'll try to give you more info later (when my
machine's sorted)

HTH
Sam

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I have several classes which I need serialized. Here is their class
definitions:

public class TopContainer
{
private LevelTwoType m_levelTwo;
public LevelTwoType LevelTwo
{
get
{
return m_levelTwo;
}
}
...
}

public class LevelTwoType
{
private ArrayList m_levelThree;

public LevelThreeType LevelThree this [int index]
{
get
{
return (LevelThreeType)m_levelThree[index];
}
}
// Plus the following
Add(LevelThreeType)
Count
...
}

there are 2 more classes similar to LevelTwoType class.

I can't get these to serialize. I tried using the ISerializable interface and implement the GetObjectData method but still no luck.

If I use the ICollection interface it sort of works but not the way I need it to.


Nov 16 '05 #6
Joe,

That's the problem. Web methods use the XmlSerializer, which doesn't
depend on the serialization attribute. It serializes all public fields and
properties (and every public property and field of every object exposed
through properties on the parent object, etc, etc).

I since XML serialization doesn't work on private members, the array
list is not serialized. Also, it doesn't handle interfaces that are exposed
through public properties either or indexers (which you have).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I have the constructor as for ISerializable as well but still no luck.

Here's some more information:
I have the [Serializable] attribute directly before the class definition
and
the class implements ISerializable.

The purpose of this is to return the TopContainer class from a WebMethod.
When I look at the web reference that is created, I see the class names
but
TopContainer doesn't contain a LevelTwoType.

TopContainer encapsulates LevelTwoType which encapsulates an arraylist,
which is an Array of LevelThreeType. LevelThreeType encapsulates another
ArrayList which is an Array of LevelFourType. LevelFourType encapsulates a
class which encapsulates an ArrayList (this class does serialize because I
use it in other places and it works fine )

All classes have the [Serializable] attribute.

I hope this isn't too confusing.

Thanks again.

"Sam Martin" <sa*********@yahoo.co.uk> wrote in message
news:#y**************@TK2MSFTNGP14.phx.gbl...
ISerialisable is definately the way forward if you want to serialise to
binary stream.

From what i remember you need a serialisation contructor (which

instantiates
the object from the serialised data).
A GetObjectData method which is used to add (member) data to a

serialisation
object

There's another method you need to implement too and serialiable class
attribute needs adding also. I can't check at moment as installing SP,

but
look in google for an example of implementing ISerialisable, you wont be

far
wrong.

If you get stuck, reply and I'll try to give you more info later (when my
machine's sorted)

HTH
Sam

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
>I have several classes which I need serialized. Here is their class
> definitions:
>
> public class TopContainer
> {
> private LevelTwoType m_levelTwo;
> public LevelTwoType LevelTwo
> {
> get
> {
> return m_levelTwo;
> }
> }
> ...
> }
>
> public class LevelTwoType
> {
> private ArrayList m_levelThree;
>
> public LevelThreeType LevelThree this [int index]
> {
> get
> {
> return (LevelThreeType)m_levelThree[index];
> }
> }
> // Plus the following
> Add(LevelThreeType)
> Count
> ...
> }
>
> there are 2 more classes similar to LevelTwoType class.
>
> I can't get these to serialize. I tried using the ISerializable interface > and implement the GetObjectData method but still no luck.
>
> If I use the ICollection interface it sort of works but not the way I need > it to.
>
>



Nov 16 '05 #7
Joe
ok - so why doesn't TopContainers, LevelTwo property get added to the
definition in the web reference? The TopContainer class shows without any
members (properties).

This is the part I don't understand.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uy**************@TK2MSFTNGP09.phx.gbl...
Joe,

That's the problem. Web methods use the XmlSerializer, which doesn't
depend on the serialization attribute. It serializes all public fields and properties (and every public property and field of every object exposed
through properties on the parent object, etc, etc).

I since XML serialization doesn't work on private members, the array
list is not serialized. Also, it doesn't handle interfaces that are exposed through public properties either or indexers (which you have).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I have the constructor as for ISerializable as well but still no luck.

Here's some more information:
I have the [Serializable] attribute directly before the class definition
and
the class implements ISerializable.

The purpose of this is to return the TopContainer class from a WebMethod. When I look at the web reference that is created, I see the class names
but
TopContainer doesn't contain a LevelTwoType.

TopContainer encapsulates LevelTwoType which encapsulates an arraylist,
which is an Array of LevelThreeType. LevelThreeType encapsulates another
ArrayList which is an Array of LevelFourType. LevelFourType encapsulates a class which encapsulates an ArrayList (this class does serialize because I use it in other places and it works fine )

All classes have the [Serializable] attribute.

I hope this isn't too confusing.

Thanks again.

"Sam Martin" <sa*********@yahoo.co.uk> wrote in message
news:#y**************@TK2MSFTNGP14.phx.gbl...
ISerialisable is definately the way forward if you want to serialise to
binary stream.

From what i remember you need a serialisation contructor (which

instantiates
the object from the serialised data).
A GetObjectData method which is used to add (member) data to a

serialisation
object

There's another method you need to implement too and serialiable class
attribute needs adding also. I can't check at moment as installing SP,

but
look in google for an example of implementing ISerialisable, you wont be
far
wrong.

If you get stuck, reply and I'll try to give you more info later (when

my machine's sorted)

HTH
Sam

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
>I have several classes which I need serialized. Here is their class
> definitions:
>
> public class TopContainer
> {
> private LevelTwoType m_levelTwo;
> public LevelTwoType LevelTwo
> {
> get
> {
> return m_levelTwo;
> }
> }
> ...
> }
>
> public class LevelTwoType
> {
> private ArrayList m_levelThree;
>
> public LevelThreeType LevelThree this [int index]
> {
> get
> {
> return (LevelThreeType)m_levelThree[index];
> }
> }
> // Plus the following
> Add(LevelThreeType)
> Count
> ...
> }
>
> there are 2 more classes similar to LevelTwoType class.
>
> I can't get these to serialize. I tried using the ISerializable

interface
> and implement the GetObjectData method but still no luck.
>
> If I use the ICollection interface it sort of works but not the way I

need
> it to.
>
>



Nov 16 '05 #8
Joe
The answer to my problem was I was missing the set method from my
properties.

Thanks to all that answered!

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:#C**************@TK2MSFTNGP10.phx.gbl...
I have several classes which I need serialized. Here is their class
definitions:

public class TopContainer
{
private LevelTwoType m_levelTwo;
public LevelTwoType LevelTwo
{
get
{
return m_levelTwo;
}
}
...
}

public class LevelTwoType
{
private ArrayList m_levelThree;

public LevelThreeType LevelThree this [int index]
{
get
{
return (LevelThreeType)m_levelThree[index];
}
}
// Plus the following
Add(LevelThreeType)
Count
...
}

there are 2 more classes similar to LevelTwoType class.

I can't get these to serialize. I tried using the ISerializable interface
and implement the GetObjectData method but still no luck.

If I use the ICollection interface it sort of works but not the way I need
it to.

Nov 16 '05 #9

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

Similar topics

0
by: psy000 | last post by:
Hi, I have a C# web service client that talks to a JAVA application sever. I use AXIS to generate the WSDL file, use wsdl.exe to generate proxy stub c# code. When I try to use c# client connect...
3
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. ...
3
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...
10
by: SStory | last post by:
My app is near completed for the basic feature of version 1.0. I have an extensive object model and I now want to persist my objects using serialization. I have chosen binaryformatter to...
4
by: Joe | last post by:
I would like to call a method of an object before the object is serialized. I currently have a surrogate that I use for deserilization and was thinking I could use one for serialization but that...
1
by: Rucha | last post by:
We are using ACAServices in our project, and are passing entity classes as parameters to the ACAServiceMethod. We are using a private variable entityState to indicate whether the entity is...
15
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with...
3
by: Zachary Turner | last post by:
Hello, I have a situation where I would like to perform custom serialization and deserialization of an existing .NET framework object (specifically, System.DateTime). Is there a common paradigm...
1
by: Paez | last post by:
Hi There. This post is a extension of thread "Serialize or not to Serialize", on 5 of December of 2006. The reason why my teacher insists that a WS cannot return a Dataset is due to low rate...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
0
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...
0
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...
0
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,...

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.