Restoring a Serialized Object on to itself

B

Bob Bartel

I have a complex "query" object that I need to save to a table and then
restore. Everything works great with the following exception. When I try
to create a method within the object called Restore() and try to restore the
object onto itself I can't: Here's the code segment:

byte[] b = Convert.FromBase64String(SavedObjectString);
MemoryStream ms = new MemoryStream(b);
BinaryFormatter bf = new BinaryFormatter();

this = (query)bf.Deserialize(ms);

As you may know the "this" reference is read-only....yet I want to be able
to restore the object within itself. Is there a way to do this?

I have successfully restored the object by applying an instance variable:

query oQuery = new query();

byte[] b = Convert.FromBase64String(SavedObjectString);
MemoryStream ms = new MemoryStream(b);
BinaryFormatter bf = new BinaryFormatter();

oQuery = (query)bf.Deserialize(ms);

But the above code must be executed in another class so its not as helpful
as it could be if I could incorporate it into my query class.

Thanks in advance.

Bob
 
M

Mufaka

Is there any particular reason that you have this requirement? If so,
your method will need to copy your deserialized object by setting
properties on your instance individually.

If you can, it seems like it would be better to just have a static
utility method for getting an instance from a serialized version.
 
S

Scott Roberts

Bob Bartel said:
I have a complex "query" object that I need to save to a table and then
restore. Everything works great with the following exception. When I try
to create a method within the object called Restore() and try to restore
the object onto itself I can't: Here's the code segment:

byte[] b = Convert.FromBase64String(SavedObjectString);
MemoryStream ms = new MemoryStream(b);
BinaryFormatter bf = new BinaryFormatter();

this = (query)bf.Deserialize(ms);

As you may know the "this" reference is read-only....yet I want to be able
to restore the object within itself. Is there a way to do this?

I have successfully restored the object by applying an instance variable:

query oQuery = new query();

byte[] b = Convert.FromBase64String(SavedObjectString);
MemoryStream ms = new MemoryStream(b);
BinaryFormatter bf = new BinaryFormatter();

oQuery = (query)bf.Deserialize(ms);

But the above code must be executed in another class so its not as helpful
as it could be if I could incorporate it into my query class.

Thanks in advance.

Bob

To expand on what Mufaka said:

public static query DeserializeQuery(string SavedObjectString)
{
byte[] b = Convert.FromBase64String(SavedObjectString);
MemoryStream ms = new MemoryStream(b);
BinaryFormatter bf = new BinaryFormatter();
return (query)bf.Deserialize(ms);
}

Not sure if that helps or not, but I'd be surprised if you can re-assign
"this" (i.e. overwrite the memory space that "this" references).
 
B

Bob Bartel

I see what you are saying. And yes, its much easier to just to use the
built-in functionality vs. a property by property restore.

Thanks anyway.

bob



Mufaka said:
Is there any particular reason that you have this requirement? If so, your
method will need to copy your deserialized object by setting properties on
your instance individually.

If you can, it seems like it would be better to just have a static utility
method for getting an instance from a serialized version.

Bob said:
I have a complex "query" object that I need to save to a table and then
restore. Everything works great with the following exception. When I
try to create a method within the object called Restore() and try to
restore the object onto itself I can't: Here's the code segment:

byte[] b = Convert.FromBase64String(SavedObjectString);
MemoryStream ms = new MemoryStream(b);
BinaryFormatter bf = new BinaryFormatter();

this = (query)bf.Deserialize(ms);

As you may know the "this" reference is read-only....yet I want to be
able to restore the object within itself. Is there a way to do this?

I have successfully restored the object by applying an instance variable:

query oQuery = new query();

byte[] b = Convert.FromBase64String(SavedObjectString);
MemoryStream ms = new MemoryStream(b);
BinaryFormatter bf = new BinaryFormatter();

oQuery = (query)bf.Deserialize(ms);

But the above code must be executed in another class so its not as
helpful as it could be if I could incorporate it into my query class.

Thanks in advance.

Bob
 

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