473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Serializable and adding fields

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
Nov 17 '05 #1
4 1529
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]
- mv*@spam.guard.caspershouse.com

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
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

Nov 17 '05 #2
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);
}

"Steve Teeples" wrote:
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

Nov 17 '05 #3
Nicholas, thanks for responding! Is there an example somewhere of how to
properly overwrite the ISerializable interface?

"Nicholas Paldino [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
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


Nov 17 '05 #4
Steve,

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

http://msdn.microsoft.com/library/de...ialization.asp
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
Nicholas, thanks for responding! Is there an example somewhere of how to
properly overwrite the ISerializable interface?

"Nicholas Paldino [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
>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


Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Magnus Byne | last post by:
Hi, I have a problem using serializable transactions. In one transaction I select a single specific row from a table using a where clause (this causes it to acquire and hold a range lock). In...
5
by: Ben Terry | last post by:
I get the following compilation error: "The type or namespace name 'Serializable' could not be found (are you missing a using directive or an assembly reference?)" Can anyone tell me why the...
3
by: Alexander Wehrli | last post by:
Hi guys What I am doing: I'm saving some configuration stuff in an Class called Options. This class has got some String, bool and an int Member. I Serialize (binary) this object in a file. ...
2
by: jeffpriz | last post by:
I'm having trouble.. I created a small class that I pass to my Pages, and I want to put that class into Viewstate.. I do: Viewstate.add("thisVar", myClassVariable) and i receive the lovely...
9
by: Developer | last post by:
Hi, How can one tell wsdl.exe/VS.NET web service proxy generatioon to to put on imported classes? For example, i fin your web service you use a class: class Data { }
2
by: Prakash | last post by:
Hi Friends, In my C# application, i have tried to serialize some structured data using Soapformatter. While calling the serialize funciton i got the exception message like "mscorlib....The...
4
by: dani k | last post by:
How do I write my own serialization of a class that has non-serializable data members? this problem arose when I tried to serialize RSAParameters, for which the private stuff is not serializable
0
mmfranke
by: mmfranke | last post by:
Hello. Hi. I'm writing a logging service that uses .NET remoting. The idea is that the service publishes an EventProcessor object that clients can access via .NET Remoting, passing it...
4
by: owen79 | last post by:
Hi, Disappointingly, I think I already know the answer to this question. I have a C# class which I'm effectively using as a storage area while I carry out a sequence of Web Method calls on a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.