You can deserialize upon object creation, like:

MyClass c = MyClass.Load(fs);
or MyClass c = new MyClass(fs);

Or if you really need an instance first before loading, you can make
MyClass a wrapper around another object type, and the Load method can
reset that field - like:

class MyClass
{
  private MyRealClass _myRealClass;
  public void Save() { /* same as before, except "_myRealClass"
instead of "this" */ }
  public void Load(FileStream fs) { /* do your deserialization stuff
here*/ }

  class MyRealClass  { /* this class is what gets serialized/
deserialized */ }
}



On Jan 24, 11:39 pm, Mark <[email protected]> wrote:
> I have a nice Save() function:
>
> // inside MyClass
> public void Save()
>         {
>             BinaryFormatter bf = new BinaryFormatter();
>             FileStream fs = new FileStream("myfile.dat",
> FileMode.Create);
>             bf.Serialize(fs, this);
>         }
>
> But what if I want to write a complimentary load function? I can't do
>
> this = bf.Deserialize(fs);
>
> So what are the alternatives?
>
> Thanks!

Reply via email to