Serialization of a custom class

J

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
S

Sam Martin

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
 
J

Joe

I believe it is Serializable. How can I tell for sure? Is there something
that would make it NOT serializable?

Thanks.

Nicholas Paldino said:
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]
- (e-mail address removed)

Joe said:
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.
 
N

Nicholas Paldino [.NET/C# MVP]

Joe,

If it doesn't have the Serializable attribute on it (LevelTwoType), then
it is not serializable.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joe said:
I believe it is Serializable. How can I tell for sure? Is there something
that would make it NOT serializable?

Thanks.

in
message news:[email protected]...
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]
- (e-mail address removed)

Joe said:
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.
 
J

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 said:
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 said:
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.
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Joe said:
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 said:
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 said:
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.
 
J

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 said:
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]
- (e-mail address removed)

Joe said:
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 said:
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

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.
 
J

Joe

The answer to my problem was I was missing the set method from my
properties.

Thanks to all that answered!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top