Deserialize Only Calls Default Constructor

C

Charles Law

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
 
C

Charles Law

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
 
C

Codo

Charles said:
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.)
 
C

Charles Law

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
 
C

Codo

Charles said:
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!
 

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