1
|
/*
|
2
|
Copyright (c) 2003-2008 Niels Kokholm and Peter Sestoft
|
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
of this software and associated documentation files (the "Software"), to deal
|
5
|
in the Software without restriction, including without limitation the rights
|
6
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
copies of the Software, and to permit persons to whom the Software is
|
8
|
furnished to do so, subject to the following conditions:
|
9
|
|
10
|
The above copyright notice and this permission notice shall be included in
|
11
|
all copies or substantial portions of the Software.
|
12
|
|
13
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
SOFTWARE.
|
20
|
*/
|
21
|
|
22
|
// C5 example: serialization and deserialization
|
23
|
|
24
|
// Compile with
|
25
|
// csc /r:C5.dll SerializationExample.cs
|
26
|
|
27
|
using System;
|
28
|
using C5;
|
29
|
using SCG = System.Collections.Generic;
|
30
|
|
31
|
using System.IO; // FileStream, Stream
|
32
|
using System.Runtime.Serialization.Formatters.Binary; // BinaryFormatter
|
33
|
|
34
|
namespace SerializationExample
|
35
|
{
|
36
|
class MyTest
|
37
|
{
|
38
|
private static readonly BinaryFormatter formatter = new BinaryFormatter();
|
39
|
|
40
|
public static void ToFile<T>(IExtensible<T> coll, String filename) {
|
41
|
Stream outstream = new FileStream(filename, FileMode.OpenOrCreate);
|
42
|
formatter.Serialize(outstream, coll);
|
43
|
outstream.Close();
|
44
|
}
|
45
|
|
46
|
public static T FromFile<T>(String filename) {
|
47
|
Stream instream = new FileStream(filename, FileMode.Open);
|
48
|
Object obj = formatter.Deserialize(instream);
|
49
|
instream.Close();
|
50
|
return (T)obj;
|
51
|
}
|
52
|
|
53
|
public static void Main() {
|
54
|
IList<String> names = new LinkedList<String>();
|
55
|
String reagan = "Reagan";
|
56
|
names.AddAll(new String[] { reagan, reagan, "Bush", "Clinton",
|
57
|
"Clinton", "Bush", "Bush" });
|
58
|
ToFile(names, "prez.bin");
|
59
|
IList<String> namesFromFile = FromFile<IList<String>>("prez.bin");
|
60
|
|
61
|
foreach (String s in namesFromFile)
|
62
|
Console.WriteLine(s);
|
63
|
|
64
|
Console.Write("Deserialization preserves sequenced equality: ");
|
65
|
Console.WriteLine(names.SequencedEquals(namesFromFile));
|
66
|
Console.Write("Deserialization reuses the same strings: ");
|
67
|
Console.WriteLine(Object.ReferenceEquals(reagan, namesFromFile[1]));
|
68
|
Console.Write("Deserialization preserves sharing: ");
|
69
|
Console.WriteLine(Object.ReferenceEquals(namesFromFile[0], namesFromFile[1]));
|
70
|
}
|
71
|
}
|
72
|
}
|