How do you set up a SerializationInfo object?

A

Adam Benson

Hi,



A SerializationInfo gets passed to your constructor for manual
deserialization.



If your deserialization constructor did this :



protected BaseConfigData(SerializationInfo info, StreamingContext context)

{

ManualDeserialize(info, context);

}



protected virtual void ManualDeserialize(SerializationInfo info,
StreamingContext context)

{

channelName = info.GetString("BC_CN");

alias = info.GetString("BC_al");

// Rest of deserialization code ...

}



And you had the object serialized in a string and wanted to re-assert the
state.

Could you set up a SerializationInfo object and call ManualDeserialize?



Something like this :



SerializationInfo info;

StreamingContext context;



byte[] b;

MemoryStream ms;



b = System.Text.Encoding.UTF8.GetBytes( serialized_state );

ms = new MemoryStream(b);

ms.Position = 0;



// How do you set info up so that it reads from ms ??

// Because here it is created blank.

// If I could point it at ms and it read its state from there I could
deserialze ms into this object. Can that be done?

info = new SerializationInfo(this.GetType(),

new System.Runtime.Serialization.FormatterConverter());

context = new
StreamingContext(System.Runtime.Serialization.StreamingContextStates.All);



this.ManualDeserialize(info, context);



TVMIA,



- Adam.


==============================
(e-mail address removed)
 
N

Nicholas Paldino [.NET/C# MVP]

Adam,

I am curious, why do you want to serialize into an existing object? The
reason that serialization uses a specialized constructor is to help avoid
sticky situations that can arise from pushing values into existing objects
on deserialization.

There really isn't an easy (or any that I can think of for that matter)
way that you can do this. All the logic for converting the bytes from the
stream to the values you need in the SerializationInfo are handled in the
BinaryFormatter, which will only call the constructor.

Could you rehydrate a new object and copy the values over?
 
A

Adam Benson

Could you rehydrate a new object and copy the values over?
Could do. Just seemed wasteful really but I guess that's what I should be
doing.

My question comes out a desire to override XML serialization in some of our
configuration objects. We have suffered a lot because any time someone
(usually a junior coder) adds or renames a field none of our configs will
load.

Manual serialization gives a lot more control and so it was our desire to
convert existing XML-serialized objects into manually-serialized objects.
The picture is complicated, however, because the in-house library code that
persists our objects to and from the database uses XML-serialization only
:-(

My hope was to alter our objects so that XML serialization was overridden
(using IXmlSerializer) and actually performed a manual serialization
resulting in just one string holding the persisted object. The overridden
XML deserialization code gets given that string and then that gets used to
populate the object.

Overall, I have to say I've found manual serialization a much easier pattern
to work with. XML serialization seems, in comparison, to be very quick to
take offense.

Thanks for the response,

Adam.
=====
 

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