Click to See Complete Forum and Search --> : FileSurfing


Steve Scott
September 5th, 2002, 11:40 PM
Is there a way to see if an object exists in a file, then overwrite it...instead of creating a new file then re-writing every object?


Also, it's my understanding that when you Deserialize an object that it calls is deserialize constructor eg...

public MyClass(SerializationInfo info, StreamingContext ctxt)
{
}

So, is it then possible to see if the object exists in the file, and then call this constructor?

Thanks,
Steve

JollyDeveloper
September 17th, 2002, 08:38 PM
Note that generally, the deserialization constructor for custom serialization (in this case, for the class MyClass) is private. Deserialization returns the top object of the deserialized graph of objects. In a file you would normally store only one object graph. You would normally do the following for deserialization :
Stream myStream = File.OpenRead(myFile);
BinaryFormatter myBinaryFormatter = new BinaryFormatter();
MyClass myClass = (MyClass)myBinaryFormatter.Deserialize(myStream);
If(typeof(myClass) != MyClass)
//do error handling