-
Notifications
You must be signed in to change notification settings - Fork 23
/
Ch24-1-RuntimeSerialization.cs
464 lines (377 loc) · 17.2 KB
/
Ch24-1-RuntimeSerialization.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//#define GetSurrogateForCyclicalReference
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Security.Permissions;
[assembly:AssemblyVersion("1.0.0.0")]
public static class RuntieSerialization {
public static void Main() {
QuickStart.Go();
UsingNonSerializedFields.Go();
OptionalField.Go();
ISerializableVersioning.Go();
SerializingSingletons.Go();
SerializationSurrogates.Go();
SerializationBinderDemo.Go();
}
}
internal static class QuickStart {
public static void Go() {
// Create a graph of objects to serialize them to the stream
var objectGraph = new List<String> { "Jeff", "Kristin", "Aidan", "Grant" };
Stream stream = SerializeToMemory(objectGraph);
// Reset everything for this demo
stream.Position = 0;
objectGraph = null;
// Deserialize the objects and prove it worked
objectGraph = (List<String>)DeserializeFromMemory(stream);
foreach (var s in objectGraph) Console.WriteLine(s);
var clone = DeepClone(objectGraph);
MultipleGraphs();
OptInSerialization();
}
private static MemoryStream SerializeToMemory(Object objectGraph) {
// Construct a stream that is to hold the serialized objects
MemoryStream stream = new MemoryStream();
// Construct a serialization formatter that does all the hard work
BinaryFormatter formatter = new BinaryFormatter();
// Tell the formatter to serialize the objects into the stream
formatter.Serialize(stream, objectGraph);
// Return the stream of serialized objects back to the caller
return stream;
}
private static Object DeserializeFromMemory(Stream stream) {
// Construct a serialization formatter that does all the hard work
BinaryFormatter formatter = new BinaryFormatter();
// Tell the formatter to deserialize the objects from the stream
return formatter.Deserialize(stream);
}
private static Object DeepClone(Object original) {
// Construct a temporary memory stream
using (MemoryStream stream = new MemoryStream()) {
// Construct a serialization formatter that does all the hard work
BinaryFormatter formatter = new BinaryFormatter();
// This line is explained in this chapter's “Streaming Contexts” section
formatter.Context = new StreamingContext(StreamingContextStates.Clone);
// Serialize the object graph into the memory stream
formatter.Serialize(stream, original);
// Seek back to the start of the memory stream before deserializing
stream.Position = 0;
// Deserialize the graph into a new set of objects and
// return the root of the graph (deep copy) to the caller
return formatter.Deserialize(stream);
}
}
[Serializable]
private sealed class Customer { /* ... */ }
[Serializable]
private sealed class Order { /* ... */ }
private static List<Customer> s_customers = new List<Customer>();
private static List<Order> s_pendingOrders = new List<Order>();
private static List<Order> s_processedOrders = new List<Order>();
private static void MultipleGraphs() {
using (var stream = new MemoryStream()) {
SaveApplicationState(stream);
stream.Position = 0;
RestoreApplicationState(stream);
}
}
private static void SaveApplicationState(Stream stream) {
// Construct a serialization formatter that does all the hard work
BinaryFormatter formatter = new BinaryFormatter();
// Serialize our application’s entire state
formatter.Serialize(stream, s_customers);
formatter.Serialize(stream, s_pendingOrders);
formatter.Serialize(stream, s_processedOrders);
}
private static void RestoreApplicationState(Stream stream) {
// Construct a serialization formatter that does all the hard work
BinaryFormatter formatter = new BinaryFormatter();
// Deserialize our application’s entire state (same order as serialized)
s_customers = (List<Customer>)formatter.Deserialize(stream);
s_pendingOrders = (List<Order>)formatter.Deserialize(stream);
s_processedOrders = (List<Order>)formatter.Deserialize(stream);
}
// Not marked [Serializable]
private struct Point { public Int32 x, y; }
private static void OptInSerialization() {
Point pt = new Point { x = 1, y = 2 };
using (var stream = new MemoryStream()) {
new BinaryFormatter().Serialize(stream, pt); // throws SerializationException
}
}
}
internal static class UsingNonSerializedFields {
[Serializable]
internal class Circle /*: IDeserializationCallback */{
private Double m_radius;
[NonSerialized]
private Double m_area;
public Circle(Double radius) {
m_radius = radius;
m_area = Math.PI * m_radius * m_radius;
}
[OnDeserialized]
private void OnDeserialized(StreamingContext context) {
m_area = Math.PI * m_radius * m_radius;
}
[OnDeserializing]
private void OnDeserializing(StreamingContext context) {
m_area = Math.PI * m_radius * m_radius;
}
//void IDeserializationCallback.OnDeserialization(Object sender) { m_area = Math.PI * m_radius * m_radius; }
}
[Serializable]
private class Outer {
public Inner inner;
[OnSerializing]
private void OnSerializing(StreamingContext context) { }
[OnSerialized]
private void OnSerialized(StreamingContext context) { }
[OnDeserializing]
private void OnDeserializing(StreamingContext context) { }
[OnDeserialized]
private void OnDeserialized(StreamingContext context) { }
}
[Serializable]
private class Inner {
[OnSerializing]
private void OnSerializing(StreamingContext context) { }
[OnSerialized]
private void OnSerialized(StreamingContext context) { }
[OnDeserializing]
private void OnDeserializing(StreamingContext context) { }
[OnDeserialized]
private void OnDeserialized(StreamingContext context) { }
}
public static void Go() {
using (var stream = new MemoryStream()) {
BinaryFormatter formatter = new BinaryFormatter();
var outer = new Outer { inner = new Inner() };
//Circle[] circles = new[] { new Circle(10), new Circle(20) };
formatter.Serialize(stream, outer);
stream.Position = 0;
outer = (Outer)formatter.Deserialize(stream);
}
}
}
internal static class OptionalField {
[Serializable]
public class Foo {
/*[OptionalField] */public String name = "jeff";
}
public static void Go() {
const String filename = @"temp.dat";
var formatter = new SoapFormatter();
// Serialize
using (var stream = File.Create(filename)) {
formatter.Serialize(stream, new Foo());
}
// Deserialize
using (var stream = File.Open(filename, FileMode.Open)) {
Foo f = (Foo)formatter.Deserialize(stream);
}
File.Delete(filename);
}
}
internal static class ISerializableVersioning {
public static void Go() {
using (var stream = new MemoryStream()) {
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, new Derived());
stream.Position = 0;
Derived d = (Derived)formatter.Deserialize(stream);
Console.WriteLine(d);
}
}
[Serializable]
private class Base {
protected String m_name = "Jeff";
protected String Name { get { return m_name; } }
public Base() { /* Make the type instantiable*/ }
}
[Serializable]
private class Derived : Base, ISerializable {
new private String m_name = "Richter";
public Derived() { /* Make the type instantiable*/ }
// If this constructor didn't exist, we'd get a SerializationException
// This constructor should be protected if this class were not sealed
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
private Derived(SerializationInfo info, StreamingContext context) {
// Get the set of serializable members for our class and base classes
Type baseType = this.GetType().BaseType;
MemberInfo[] mi = FormatterServices.GetSerializableMembers(baseType, context);
// Deserialize the base class's fields from the info object
for (Int32 i = 0; i < mi.Length; i++) {
// Get the field and set it to the deserialized value
FieldInfo fi = (FieldInfo)mi[i];
fi.SetValue(this, info.GetValue(baseType.FullName + "+" + fi.Name, fi.FieldType));
}
// Deserialize the values that were serialized for this class
m_name = info.GetString("Name");
}
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) {
// Serialize the desired values for this class
info.AddValue("Name", m_name);
// Get the set of serializable members for our class and base classes
Type baseType = this.GetType().BaseType;
MemberInfo[] mi = FormatterServices.GetSerializableMembers(baseType, context);
// Serialize the base class's fields to the info object
for (Int32 i = 0; i < mi.Length; i++) {
// Prefix the field name with the fullname of the base type
info.AddValue(baseType.FullName + "+" + mi[i].Name, ((FieldInfo)mi[i]).GetValue(this));
}
}
public override String ToString() {
return String.Format("Base Name={0}, Derived Name={1}", base.Name, m_name);
}
}
}
internal static class SerializingSingletons {
public static void Go() {
// Create an array with multiple elements refering to the one Singleton object
Singleton[] a1 = { Singleton.GetSingleton(), Singleton.GetSingleton() };
Console.WriteLine("Do both array elements refer to the same object? " + (a1[0] == a1[1])); // "True"
using (var stream = new MemoryStream()) {
BinaryFormatter formatter = new BinaryFormatter();
// Serialize and then deserialize the array elements
formatter.Serialize(stream, a1);
stream.Position = 0;
Singleton[] a2 = (Singleton[])formatter.Deserialize(stream);
Console.WriteLine("Do both array elements refer to the same object? " + (a2[0] == a2[1])); // "True"
Console.WriteLine("Do all array elements refer to the same object? " + (a1[0] == a2[0])); // "True"
}
}
// There should be only one instance of this type per AppDomain
[Serializable]
public sealed class Singleton : ISerializable {
// This is the one instance of this type
private static readonly Singleton s_theOneObject = new Singleton();
// Here are the instance fields
public String Name = "Jeff";
public DateTime Date = DateTime.Now;
// Private constructor allowing this type to construct the singleton
private Singleton() { }
// Method returning a reference to the singleton
public static Singleton GetSingleton() { return s_theOneObject; }
// Method called when serializing a Singleton
// I recommend using an Explicit Interface Method Impl. here
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
info.SetType(typeof(SingletonSerializationHelper));
// No other values need to be added
}
[Serializable]
private sealed class SingletonSerializationHelper : IObjectReference {
// Method called after this object (which has no fields) is deserialized
public Object GetRealObject(StreamingContext context) {
return Singleton.GetSingleton();
}
}
// NOTE: The special constructor is NOT necessary because it's never called
}
}
internal static class SerializationSurrogates {
public static void Go() {
using (var stream = new MemoryStream()) {
// 1. Construct the desired formatter
IFormatter formatter = new SoapFormatter();
// 2. Construct a SurrogateSelector object
SurrogateSelector ss = new SurrogateSelector();
// 3. Tell the surrogate selector to use our surrogate for DateTime objects
ISerializationSurrogate utcToLocalTimeSurrogate = new UniversalToLocalTimeSerializationSurrogate();
#if GetSurrogateForCyclicalReference
utcToLocalTimeSurrogate = FormatterServices.GetSurrogateForCyclicalReference(utcToLocalTimeSurrogate);
#endif
ss.AddSurrogate(typeof(DateTime), formatter.Context, utcToLocalTimeSurrogate);
// NOTE: AddSurrogate can be called multiple times to register multiple surrogates
// 4. Tell the formatter to use our surrogate selector
formatter.SurrogateSelector = ss;
// Create a DateTime that represents the local time on the machine & serialize it
DateTime localTimeBeforeSerialize = DateTime.Now;
formatter.Serialize(stream, localTimeBeforeSerialize);
// The Soap-formatted stream displays the Universal time as a string to prove it worked
stream.Position = 0;
Console.WriteLine(new StreamReader(stream).ReadToEnd());
// Deserialize the Universal time string & convert it to a local DateTime for this machine
stream.Position = 0;
DateTime localTimeAfterDeserialize = (DateTime)formatter.Deserialize(stream);
// Prove it worked correctly:
Console.WriteLine("LocalTimeBeforeSerialize ={0}", localTimeBeforeSerialize);
Console.WriteLine("LocalTimeAfterDeserialize={0}", localTimeAfterDeserialize);
}
}
private sealed class UniversalToLocalTimeSerializationSurrogate : ISerializationSurrogate {
public void GetObjectData(Object obj, SerializationInfo info, StreamingContext context) {
info.AddValue("Date", ((DateTime)obj).ToUniversalTime().ToString("u")); // Convert the DateTime from local to UTC
}
public Object SetObjectData(Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) {
DateTime dt = DateTime.ParseExact(info.GetString("Date"), "u", null).ToLocalTime(); // Convert the DateTime from UTC to local
#if GetSurrogateForCyclicalReference
// When using GetSurrogateForCyclicalReference, you must modify 'obj' directly and return null or obj
// So, I modify the boxed DateTime that is passed into SetObjectData
FieldInfo fi = typeof(DateTime).GetField("dateData", BindingFlags.NonPublic | BindingFlags.Instance);
fi.SetValue(obj, fi.GetValue(dt));
return null;
#else
return dt;
#endif
}
}
}
internal static class SerializationBinderDemo {
public static void Go() {
using (var stream = new MemoryStream()) {
IFormatter formatter = new BinaryFormatter();
formatter.Binder = new Ver1ToVer2SerializationBinder();
formatter.Serialize(stream, new Ver1());
stream.Position = 0;
Ver2 t = (Ver2)formatter.Deserialize(stream);
Console.WriteLine("Type deserialized={0}, ToString={{{1}}}", t.GetType(), t);
}
}
[Serializable]
private sealed class Ver1 {
public Int32 x = 1, y = 2, z = 3;
}
[Serializable]
private sealed class Ver2 : ISerializable {
Int32 a, b, c;
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public void GetObjectData(SerializationInfo info, StreamingContext context) {
/* Never called: do nothing */
}
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
private Ver2(SerializationInfo info, StreamingContext context) {
a = info.GetInt32("x");
b = info.GetInt32("y");
c = info.GetInt32("z");
}
public override string ToString() {
return String.Format("a={0}, b={1}, c={2}", a, b, c);
}
}
private sealed class Ver1ToVer2SerializationBinder : SerializationBinder {
public override void BindToName(Type serializedType, out string assemblyName, out string typeName) {
assemblyName = Assembly.GetExecutingAssembly().FullName;
typeName = typeof(Ver2).FullName;
}
public override Type BindToType(String assemblyName, String typeName) {
// Deserialize any Ver1 object from version 1.0.0.0 of this assembly into a Ver2 object
// Calculate the assembly name that defined the Ver1 type
AssemblyName assemVer1 = Assembly.GetExecutingAssembly().GetName();
assemVer1.Version = new Version(1, 0, 0, 0);
// If deserializing the Ver1 object from v1.0.0.0 of our assembly, turn it into a Ver2 object
if (assemblyName == assemVer1.ToString() && typeName == "SerializationBinderDemo+Ver1")
return typeof(Ver2);
// Else, just return the same type being requested
return Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
}
}
}
///////////////////////////////////////////////////////////////////////////////