Note: the original book is translated as serialized,MsdnTranslation for serialization, I useMsdnAs serialized.
You can use attributes (Attribute) Mark the elements of the class as serializable (Serializable) And cannot be serialized(Nonserialized).. NetThere are two classes to implementIformatterIn the excuse classSerializeAndDeserializeMethod:BinaryformatterAndSoapformatter. The difference between the two classes is that the data stream format is different.
UseBinaryformatterSerialization
In the following example, we create a custom type (Insect) Set, useBinaryformatterWrite them to binary files and then read them back.
Note:ProgramYou need to import some namespaces:
Using system;
Using system. IO;
Using system. collections;
Using system. runtime. serialization;
Using system. runtime. serialization. formatters. Binary;
[Serializable]
Public Class Insect
{
Private String Name;
[Nonserialized]
Private Int ID;
Public Insect ( String Name, Int ID)
{
This. Name=Name;
This. ID=ID;
}
Public Override String Tostring ()
{
ReturnString. Format ("{0 }:{ 1}", Name, ID );
}
}
We use a standard attributeInsectClass declaration is serializable. However, this field cannot be persisted because it is declared as non-serializable.
Let's first perform a test. We only instantiate oneInsectObject, create a file, and then useBinaryformatterObject andSerializeWrite thisInsectObject:
Class Serializeapp
{
Public Static Void Main ( String [] ARGs)
{
Insect I = New Insect ( " Meadow Brown " , 12 );
Stream SW = File. Create ( " Insects. Bin " );
Binaryformatter BF = New Binaryformatter ();
BF. serialize (SW, I );
Sw. Close ();
}
}
IfVisual StudioOpenInsect. BinThe file will see the following content:
Fbinaryformatter, version = 0.0.0 . 0, culture = neutral, publickeytoken = NULL insect name Meadow Brown(Because I have no software, this is only part of the content)
We can note thatIDField because it is not serialized.
Now, we have added severalInsectObject.
Arraylist box = New Arraylist ();
Box. Add ( New Insect ( " Marsh fritillary " , 34 ));
Box. Add ( New Insect ( " Speckled wood " , 56 ));
Box. Add ( New Insect ( " Milkweed " , 78 ));
SW = File. Open ( " Insects. Bin " , Filemode. append );
BF. serialize (SW, box );
Sw. Close ();
Stream SR = File. openread ( " Insects. Bin " );
Insect J = (INSECT) BF. deserialize (SR );
Console. writeline (j );
Arraylist bag = (Arraylist) BF. deserialize (SR );
Sr. Close ();
Foreach (Insect K In Bag)
{
Console. writeline (k );
}
The output of this program is as follows:
Meadow Brown: 0
Marsh fritillary: 0
Speckled WOOD: 0
Milkweed: 0
The id value is 0, and the cause is obvious (it is initialized to 0 during the insect construction in the foreach loop ).
Note: We carefully read back an insect object-the object that has been serialized to the file before reading the collection.
In addition, when we use deserialize, We must convert the type of the returned object, because this method returns a general object.
The Set added later contains three insect data, which saves some overhead because you only need to record the insect class type information for the first column.
Another interesting thing is that the serialization mechanism can obviously read and write private fields in the column.