[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.remoting

Serialization with abstract class?

gaetan

8/5/2004 10:48:00 AM

Hello

I have a little problem about serialization.
I have a base class and a field within it i WANT to set to a specific
value when deserializing. So i did :
interface class IClassA
{
// classic interface
}

[Serializable()]
public abstract class ClassABase : IClassA, ISerializable
{
int i1;
int i2;
int i3; // => i want to set this to 0 in the deserialized object

public ClassABase (SerializationInfo info, StreamingContext context)
{
i1= info.GetInt32("i1");
i2 = info.GetString("i2");
i3 = 0; // set this field to a specific value
}

public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("i1", i1);
info.AddValue("i2", i2);
// or i should have done that :
// info.AddValue("i3", 0);
}
}

anyway now i want to inherit this base class into a serializable
class:

[Serializable()]
public class ClassA : ClassABase
{
// have its own to-be-serialized fields
}

The serialization seem to go well (no exception). But when i want to
deserialized i got an exception:
The constructor to deserialize an object of type ClassA was not found.
So a public constructor is requiered:

public ClassA (SerializationInfo info, StreamingContext context)
: base(info, context)
{ }

but this way the fields in ClassA are not deserialized...
I would like to avoid writing the
public ClassA (SerializationInfo info, StreamingContext context)
and
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
function for the ClassA since it "can" be done by the
"[Serializable()]"...

any suggestion?

Thanks

Great A'Tan -- http://www.xeberon.n...
3 Answers

Sunny

8/5/2004 2:13:00 PM

0

Hi,

In article <9712e9c6.0408050247.7ce43403@posting.google.com>,
gaetan@xeberon.net says...
> Hello
>
> I have a little problem about serialization.
> I have a base class and a field within it i WANT to set to a specific
> value when deserializing. So i did :
> interface class IClassA
> {
> // classic interface
> }
>
> [Serializable()]
> public abstract class ClassABase : IClassA, ISerializable
> {
> int i1;
> int i2;
> int i3; // => i want to set this to 0 in the deserialized object
>
> public ClassABase (SerializationInfo info, StreamingContext context)
> {
> i1= info.GetInt32("i1");
> i2 = info.GetString("i2");
> i3 = 0; // set this field to a specific value
> }
>
> public virtual void GetObjectData(SerializationInfo info,
> StreamingContext context)
> {
> info.AddValue("i1", i1);
> info.AddValue("i2", i2);
> // or i should have done that :
> // info.AddValue("i3", 0);
> }
> }
>
> anyway now i want to inherit this base class into a serializable
> class:
>
> [Serializable()]
> public class ClassA : ClassABase
> {
> // have its own to-be-serialized fields
> }
>
> The serialization seem to go well (no exception). But when i want to
> deserialized i got an exception:
> The constructor to deserialize an object of type ClassA was not found.
> So a public constructor is requiered:
>
> public ClassA (SerializationInfo info, StreamingContext context)
> : base(info, context)
> { }
>
> but this way the fields in ClassA are not deserialized...
> I would like to avoid writing the
> public ClassA (SerializationInfo info, StreamingContext context)
> and
> public virtual void GetObjectData(SerializationInfo info,
> StreamingContext context)
> function for the ClassA since it "can" be done by the
> "[Serializable()]"...
>
> any suggestion?
>
> Thanks
>
> Great A'Tan -- http://www.xeberon.n...
>

If you implement ISerializable, the formatter knows that you want your
class to do the serialization. If your class not implements it, and is
only marked [Serializable], then the formatter will use reflection to
serialize the data. There is no way to tell the formatter to use both
approaches. You have to implement your serialization constructors like:

public ClassA (SerializationInfo info, StreamingContext context)
{
classAfield = info.GetInt32("classAfield");
base(info, context);
}

public override void GetObjectData((SerializationInfo info,
StreamingContext context)
{
info.AddValue("classAfield", classAfield);
base.GetObjectData(info, context);
}

Sunny

Gaetan semet

8/5/2004 2:28:00 PM

0


Thanks, it's what i've done finally...
is there no other way to define a specific value for a certain field
after deserialization?

-----
Great A'Tan - http://www.xeberon....

*** Sent via Developersdex http://www.develop... ***
Don't just participate in USENET...get rewarded for it!

Sam Santiago

8/5/2004 2:33:00 PM

0

You must implement the methods in a derived class. Check out this link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconCustomSeriali...

Here's an excerpt:
"When you derive a new class from one that implements ISerializable, the
derived class must implement both the constructor as well as the
GetObjectData method if it has variables that need to be serialized."

If you only want i3 to be zero you might consider not implementing the
ISerializable interface and marking i3 as not serializable with the
NonSerialized attribute:

[NonSerialized] int i3;

Thanks,

Sam


--
_______________________________
Sam Santiago
ssantiago@n0spam-SoftiTechture.com
http://www.SoftiTe...
_______________________________
"Gaetan" <gaetan@xeberon.net> wrote in message
news:9712e9c6.0408050247.7ce43403@posting.google.com...
> Hello
>
> I have a little problem about serialization.
> I have a base class and a field within it i WANT to set to a specific
> value when deserializing. So i did :
> interface class IClassA
> {
> // classic interface
> }
>
> [Serializable()]
> public abstract class ClassABase : IClassA, ISerializable
> {
> int i1;
> int i2;
> int i3; // => i want to set this to 0 in the deserialized object
>
> public ClassABase (SerializationInfo info, StreamingContext context)
> {
> i1= info.GetInt32("i1");
> i2 = info.GetString("i2");
> i3 = 0; // set this field to a specific value
> }
>
> public virtual void GetObjectData(SerializationInfo info,
> StreamingContext context)
> {
> info.AddValue("i1", i1);
> info.AddValue("i2", i2);
> // or i should have done that :
> // info.AddValue("i3", 0);
> }
> }
>
> anyway now i want to inherit this base class into a serializable
> class:
>
> [Serializable()]
> public class ClassA : ClassABase
> {
> // have its own to-be-serialized fields
> }
>
> The serialization seem to go well (no exception). But when i want to
> deserialized i got an exception:
> The constructor to deserialize an object of type ClassA was not found.
> So a public constructor is requiered:
>
> public ClassA (SerializationInfo info, StreamingContext context)
> : base(info, context)
> { }
>
> but this way the fields in ClassA are not deserialized...
> I would like to avoid writing the
> public ClassA (SerializationInfo info, StreamingContext context)
> and
> public virtual void GetObjectData(SerializationInfo info,
> StreamingContext context)
> function for the ClassA since it "can" be done by the
> "[Serializable()]"...
>
> any suggestion?
>
> Thanks
>
> Great A'Tan -- http://www.xeberon.n...