Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Restoring a Serialized Object on to itself

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Bartel - 22 Feb 2008 21:31 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
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).

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.