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
Mufaka - 22 Feb 2008 21:48 GMT
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.
> 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
[quoted text clipped - 26 lines]
>
> Bob
Bob Bartel - 22 Feb 2008 22:13 GMT
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
> 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
[quoted text clipped - 33 lines]
>>
>> Bob
Scott Roberts - 22 Feb 2008 21:57 GMT
>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
[quoted text clipped - 26 lines]
>
> 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).