Serializable and adding fields

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a simple class that is Serializable() so that I can read/write it
to/from disk. Functionally it works great. However, I soon learned that if
I added a field or property to the class that I could no longer read the
"old" data that was written. The error I get says that the component number
no longer matches. Is there a way to tell the reader to ignore the mismatch
and continue reading the data that is currently in the file?
 
Steve,

In order to do this, you will have to implement the ISerializable
interface and handle the writing of the values yourself. If the field
doesn't exist, then you can just ignore it in your implementation.

I'm not sure, but I believe that there is a new attribute in .NET 2.0
which will allow you to specify that certain fields can be ignored if they
are not found.

Hope this helps.
 
I think you may be able to do this by implementing the ISerializable
interface and adding a serialization constructor. You may still have
problems with versioning though.

protected MyClass(SerializationInfo info, StreamingContext context)
{
this.MyString = info.GetString("MyString");
this.MyInteger = info.GetInt32("MyInteger");
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("MyString", string);
info.AddValue("MyInteger", int);
}
 
Nicholas, thanks for responding! Is there an example somewhere of how to
properly overwrite the ISerializable interface?

Nicholas Paldino said:
Steve,

In order to do this, you will have to implement the ISerializable
interface and handle the writing of the values yourself. If the field
doesn't exist, then you can just ignore it in your implementation.

I'm not sure, but I believe that there is a new attribute in .NET 2.0
which will allow you to specify that certain fields can be ignored if they
are not found.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve Teeples said:
I have a simple class that is Serializable() so that I can read/write it
to/from disk. Functionally it works great. However, I soon learned that
if
I added a field or property to the class that I could no longer read the
"old" data that was written. The error I get says that the component
number
no longer matches. Is there a way to tell the reader to ignore the
mismatch
and continue reading the data that is currently in the file?
 
Steve,

Check out the section of the .NET framework documentation titled "Custom
Serialization" located at (watch for line wrap):

http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconCustomSerialization.asp


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve Teeples said:
Nicholas, thanks for responding! Is there an example somewhere of how to
properly overwrite the ISerializable interface?

Nicholas Paldino said:
Steve,

In order to do this, you will have to implement the ISerializable
interface and handle the writing of the values yourself. If the field
doesn't exist, then you can just ignore it in your implementation.

I'm not sure, but I believe that there is a new attribute in .NET 2.0
which will allow you to specify that certain fields can be ignored if
they
are not found.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve Teeples said:
I have a simple class that is Serializable() so that I can read/write it
to/from disk. Functionally it works great. However, I soon learned
that
if
I added a field or property to the class that I could no longer read
the
"old" data that was written. The error I get says that the component
number
no longer matches. Is there a way to tell the reader to ignore the
mismatch
and continue reading the data that is currently in the file?
 
Back
Top