PC Review


Reply
Thread Tools Rate Thread

Deserialize Only Calls Default Constructor

 
 
Charles Law
Guest
Posts: n/a
 
      12th Aug 2004
I have an object that is created like this

Dim MyObj As MyClass

MyObj = New MyClass(objInfo)

The point of this being that the creation of the object requires some
information so that it can set some internal parameters.

When the object is serialised, its state is saved; but not the internal
stuff that was derived from objInfo, because it comes from somewhere else.
So far so good.

When I deserialise the object, it is effectively created like this

MyObj = New MyClass()

so it has to instantiate itself without the objInfo stuff, and therein lies
my problem.

Is there a straight forward way round this, or again is there a pattern that
addresses this problem?

TIA

Charles


 
Reply With Quote
 
 
 
 
Charles Law
Guest
Posts: n/a
 
      16th Aug 2004
Hi guys

I was just wondering whether I have become invisible, or do I just ask
awkward questions?

I had hoped that someone would be able to say "oh, you just do this", or
"look at that pattern", but I guess not.

Any thoughts or musings would be welcome.

Thanks.

Charles


"Charles Law" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> I have an object that is created like this
>
> Dim MyObj As MyClass
>
> MyObj = New MyClass(objInfo)
>
> The point of this being that the creation of the object requires some
> information so that it can set some internal parameters.
>
> When the object is serialised, its state is saved; but not the internal
> stuff that was derived from objInfo, because it comes from somewhere else.
> So far so good.
>
> When I deserialise the object, it is effectively created like this
>
> MyObj = New MyClass()
>
> so it has to instantiate itself without the objInfo stuff, and therein

lies
> my problem.
>
> Is there a straight forward way round this, or again is there a pattern

that
> addresses this problem?
>
> TIA
>
> Charles
>
>



 
Reply With Quote
 
Codo
Guest
Posts: n/a
 
      16th Aug 2004
Charles Law wrote:

> When the object is serialised, its state is saved; but not the internal
> stuff that was derived from objInfo, because it comes from somewhere else.
> So far so good.

I wouldn't say that. You have lost data and you wont be able to recreate
the object.

>
> When I deserialise the object, it is effectively created like this
>
> MyObj = New MyClass()

No. It will be created by the constructor
New(info as serializationInfo, context as StreamingContext)

>
> so it has to instantiate itself without the objInfo stuff, and therein
> lies my problem.

Your problem was in step 1...

>
> Is there a straight forward way round this, or again is there a pattern
> that addresses this problem?
>

Yes, you have to serialise EVERYTHING. (Or something that will allow you to
reconstruct it.)

> TIA
>
> Charles


 
Reply With Quote
 
Charles Law
Guest
Posts: n/a
 
      16th Aug 2004
Hi Codo

Thanks for the reply. I have been experimenting a bit since reading your
comments, and have looked a few things up in the MSDN too.

I tried implementing a constructor like the one you mention, to see if it
gets called, but it didn't.

I use the xml serializer and mark my classes <Serializable()>, rather than
implement ISerializable. Does that make a difference?

Charles


"Codo" <(E-Mail Removed)> wrote in message
news:Fv0Uc.78$(E-Mail Removed)...
> Charles Law wrote:
>
> > When the object is serialised, its state is saved; but not the internal
> > stuff that was derived from objInfo, because it comes from somewhere

else.
> > So far so good.

> I wouldn't say that. You have lost data and you wont be able to recreate
> the object.
>
> >
> > When I deserialise the object, it is effectively created like this
> >
> > MyObj = New MyClass()

> No. It will be created by the constructor
> New(info as serializationInfo, context as StreamingContext)
>
> >
> > so it has to instantiate itself without the objInfo stuff, and therein
> > lies my problem.

> Your problem was in step 1...
>
> >
> > Is there a straight forward way round this, or again is there a pattern
> > that addresses this problem?
> >

> Yes, you have to serialise EVERYTHING. (Or something that will allow you

to
> reconstruct it.)
>
> > TIA
> >
> > Charles

>



 
Reply With Quote
 
Codo
Guest
Posts: n/a
 
      18th Aug 2004
Charles Law wrote:

> Hi Codo
>
> Thanks for the reply. I have been experimenting a bit since reading your
> comments, and have looked a few things up in the MSDN too.
>
> I tried implementing a constructor like the one you mention, to see if it
> gets called, but it didn't.
>
> I use the xml serializer and mark my classes <Serializable()>, rather than
> implement ISerializable. Does that make a difference?
>
> Charles
>


I think it does, though I haven't tested them. I have been implementing
ISerializable in vb.net, because I started to have problems with events
(you will LOOOOOOOVE events and <Serializable()>. You will want to move to
C#). I think the constructor New(info as SerializationInfo, context as
StreamingContext) only gets called when you implement ISerializable.
Otherwise dotnet will execute some funny internal (you may want ask Bill
Gates) reconstruction of your objects. Try reading ISerialisable on the
help veeeery slowly. It has lots of things that can confuse you very
easily (I was)... Hope this helps!
 
Reply With Quote
 
Charles Law
Guest
Posts: n/a
 
      18th Aug 2004
Hi Codo

I will proceed with caution ... thanks.

Charles


"Codo" <(E-Mail Removed)> wrote in message
news:yAPUc.357$(E-Mail Removed)...
> Charles Law wrote:
>
> > Hi Codo
> >
> > Thanks for the reply. I have been experimenting a bit since reading your
> > comments, and have looked a few things up in the MSDN too.
> >
> > I tried implementing a constructor like the one you mention, to see if

it
> > gets called, but it didn't.
> >
> > I use the xml serializer and mark my classes <Serializable()>, rather

than
> > implement ISerializable. Does that make a difference?
> >
> > Charles
> >

>
> I think it does, though I haven't tested them. I have been implementing
> ISerializable in vb.net, because I started to have problems with events
> (you will LOOOOOOOVE events and <Serializable()>. You will want to move

to
> C#). I think the constructor New(info as SerializationInfo, context as
> StreamingContext) only gets called when you implement ISerializable.
> Otherwise dotnet will execute some funny internal (you may want ask Bill
> Gates) reconstruction of your objects. Try reading ISerialisable on the
> help veeeery slowly. It has lots of things that can confuse you very
> easily (I was)... Hope this helps!



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deserialize in constructor jhcorey@yahoo.com Microsoft C# .NET 2 12th Jun 2006 09:17 PM
The constructor to deserialize an object of type '...' was not found Dan Holmes Microsoft C# .NET 1 24th Feb 2006 10:07 PM
Exception in constructor calls destructor???? =?Utf-8?B?SkFM?= Microsoft VC .NET 8 1st Jan 2006 09:54 PM
Can a constructor deserialize to itself? Stephen Travis Microsoft VB .NET 1 14th Jul 2004 06:38 PM
LicenseManager calls constructor 4 times if exception thrown?? psd_vbnet Microsoft Dot NET Framework Forms 0 7th Dec 2003 01:09 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:39 PM.