Serialization of the class derived from CollectionBase

J

Just D.

All,

1. Did anybody write Serialization/Deserialization of some custom class
derived
from the CollectionBase class? The custom class is like a container of many
different simple classes, each one of them is serializable, these methods
have been added and work properly.

As an additional headache this custom class can also include a few object
like collection classes.

What is the better way to get this CollectionClass serialized into XML
string and then restore it from the XML string if required? To serialize all
embedded objects one by one, return string and use this string to serialize
the object at the higher level?

This class implements Add(), Remove(), IndexOf(), Append() and other
methods.

--------------------------------------------------------------------------------------------------------------

2. The second question is:

The class has many private or protected members, almost each one has a
public property. Can we exclude some of these public properties from
serialization? I tried to add
[NonSerialized], but the VS writes the following:

Applications\CPhones.cs(396): Attribute 'NonSerialized' is not valid on this
declaration type. It is valid on 'field' declarations only.

All I tried to do was to add [NonSerialized] before public property.

private Int64 m_iOwnerID = -1;
[NonSerialized]
public int OwnerType
{
get
{
return m_iOwnerType;
}
set
{
m_iOwnerType = value;
}
} // End OwnerType Property

Just D.
 
R

ranjan.listserv

What is the better way to get this CollectionClass serialized into XML
string and then restore it from the XML string if required?

Create an XML Node of which, each child node will represent an object in
the collection, and then write similar sub-nodes customized for each class.
This will help you deserialize easily. Ofcourse, this is just a blue print,
you might need to build on this.
All I tried to do was to add [NonSerialized] before public property.

private Int64 m_iOwnerID = -1;
[NonSerialized]
public int OwnerType
{
get
{
return m_iOwnerType;
}
set
{
m_iOwnerType = value;
}
} // End OwnerType Property


A property basically is a method (get_OwnerType if you view the IL/Reflect
on the dll, for the accessor, set_ for the mutator). This is why the
compiler throws an exception. The built-in serializer reads all public
properties (because they contain the state of the class) and serializes
them. You can call it a flaw in the design (I will be happy to know why,if
its by design) because this cannot be suppressed because of the above
mentioned error.
The answer is, you need to implement ISerializable, and write your own code
to suppress/ allow access to these properties.
 
R

ranjan.listserv

On Fri, 28 Jan 2005 14:49:29 +0530, (e-mail address removed) wrote:
You can use [XMLIgnore] over the properties also.

Ranjan
 

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