Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serialize this

Status
Not open for further replies.

ThankGodItsFriday

Programmer
May 12, 2005
11
SE
Hi!

I'd like to serialize an object from within itself.

Code:
public class person
{
   public int id = 5;
   public ArrayList phoneNumbers;
   ...

   public void Serialize (string filename)
   {
      //Code to serialize current object
   }
}

the code
Code:
XmlSerializer serializer = new XmlSerializer(typeof(Whatever));
...

serializer.Serialize(writer, this);

Generates the runtime error "An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll"

Is this possible to do w/o creating a copy of the current object and serialize that?

 
Maybe you have to initalize the arraylist first.

public ArrayList phoneNumbers = new ArrayList();


--- neteject.com - Internet Solutions ---
 
Something like this

public object Serialize ()
{
MemoryStream buffer = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
Serialization.SerializationNotification.OnSerializing(this);
formatter.Serialize(buffer, this);
return buffer;
}
 
Actually I made a mistake... [doh]

It helps if the object given in the typeof() declaration is the same object you are trying to serialize... :)

Thnx anyways!!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top