Information Technology Reference
In-Depth Information
private
MyType(
SerializationInfo
info,
StreamingContext
cntxt)
{
}
private string
label;
[
NonSerialized
]
private int
value;
private
OtherClass
otherThing;
// Added in version 2
// The runtime throws Exceptions
// with it finds this field missing in version 1.0
// files.
private int
value2;
}
Yo u a d d s u p p o r t f o r I S e r i a l i z a b l e t o a d d r e s s t h i s b e h a v i o r. T h e I S e r i a l i z a b l e
interface defines one method, but you have to implement two. ISerializable
defines the GetObjectData() method that is used to write data to a stream.
In addition, you must provide a serialization constructor to initialize the
object from the stream:
private
MyType(
SerializationInfo
info,
StreamingContext
cntxt)
The serialization constructor in the following class shows how to read a
previous version of the type and read the current version consistently with
the default implementation generated by adding the Serializable attribute:
using global::
System.Runtime.Serialization;
using global::
System.Security.Permissions;
[
Serializable
]
public sealed class
MyType
:
ISerializable
{
private string
label;
[
NonSerialized
]
private int
value;