-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathObjectStreamer.cs
More file actions
347 lines (274 loc) · 10.3 KB
/
ObjectStreamer.cs
File metadata and controls
347 lines (274 loc) · 10.3 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Reflection;
namespace Grammophone.Serialization
{
/// <summary>
/// Common base for <see cref="ObjectReader"/> and <see cref="ObjectWriter"/>.
/// Holds formatter state.
/// </summary>
internal abstract class ObjectStreamer
{
#region Auxilliary types
/// <summary>
/// Used as a key for searching methods of a type which are marked with an attribute.
/// </summary>
private struct AttributedMethodSearch
{
public Type HolderType;
public Type AttributeType;
public override int GetHashCode()
{
return 23 * this.AttributeType.GetHashCode() + HolderType.GetHashCode();
}
}
#endregion
#region Private fields
/// <summary>
/// The serialization binder as specified by the formatter.
/// </summary>
/// <remarks>
/// This is provided by the formatter parameter in the constructor.
/// </remarks>
protected SerializationBinder binder;
/// <summary>
/// The streaming context as specified by the formatter.
/// </summary>
/// <remarks>
/// This is provided by the formatter parameter in the constructor.
/// </remarks>
protected StreamingContext context;
/// <summary>
/// The surrogate selector of the formatter.
/// </summary>
/// <remarks>
/// This is provided by the formatter parameter in the constructor.
/// </remarks>
protected ISurrogateSelector surrogateSelector;
/// <summary>
/// The stream used for serializing or deserializing objects.
/// </summary>
protected Stream stream;
/// <summary>
/// The type converter as specified by the formatter.
/// </summary>
/// <remarks>
/// This is provided by the formatter parameter in the constructor.
/// </remarks>
protected IFormatterConverter converter;
/// <summary>
/// An empty object array commonly used for invoking parameterless methods or constructors
/// using reflection.
/// </summary>
protected object[] emptyParameters;
/// <summary>
/// Maps types to serializable members via reflection,
/// excluding those marked with <see cref="NonSerializedAttribute"/>.
/// </summary>
private Dictionary<Type, MemberInfo[]> serializableMembersByType;
/// <summary>
/// Holds a dictionary of atributed methods keyed by holder type and attribute type.
/// </summary>
private Dictionary<AttributedMethodSearch, IList<MethodInfo>> attributedMethodsDictionary;
/// <summary>
/// A cache holding surrogates of types. If a type does not have a surrogate,
/// the corresponding entry's value is null.
/// </summary>
private Dictionary<Type, ISerializationSurrogate> surrogatesByType;
#endregion
#region Construction
/// <summary>
/// Create.
/// </summary>
/// <param name="stream">The stream to read or write.</param>
/// <param name="formatter">The formatter holding the serialization properties.</param>
public ObjectStreamer(Stream stream, FastBinaryFormatter formatter)
{
if (stream == null) throw new ArgumentNullException("stream");
if (formatter == null) throw new ArgumentNullException("formatter");
this.stream = stream;
this.binder = formatter.Binder;
this.context = formatter.Context;
this.surrogateSelector = formatter.SurrogateSelector;
this.converter = formatter.Converter;
this.emptyParameters = new object[0];
this.serializableMembersByType = new Dictionary<Type, MemberInfo[]>();
this.attributedMethodsDictionary = new Dictionary<AttributedMethodSearch, IList<MethodInfo>>();
this.surrogatesByType = new Dictionary<Type, ISerializationSurrogate>();
}
#endregion
#region Protected methods
/// <summary>
/// Make a list large enough to accommodate <paramref name="lastIndex"/>, padding with the
/// default objects of type <typeparamref name="T"/> as necessary.
/// </summary>
/// <typeparam name="T">The type of items in the list.</typeparam>
/// <param name="list">The list.</param>
/// <param name="lastIndex">The desired minimum last index of the list.</param>
protected static void AccommodateList<T>(List<T> list, int lastIndex)
{
if (list == null) throw new ArgumentNullException("list");
for (int i = list.Count; i <= lastIndex; i++)
{
list.Add(default(T));
}
}
/// <summary>
/// Visit each array element and perform an action.
/// </summary>
/// <param name="array">The array whose elements to visit.</param>
/// <param name="action">The action to perform as a function of the array and the element index.</param>
protected static void VisitArrayElements(Array array, Action<Array, int[]> action)
{
if (array == null) throw new ArgumentNullException("array");
if (action == null) throw new ArgumentNullException("action");
int dimensions = array.Rank;
int[] indices = new int[dimensions];
for (int i = 0; i < indices.Length; i++)
{
var lowerBound = array.GetLowerBound(i);
var upperBound = array.GetUpperBound(i);
if (lowerBound > upperBound) return;
indices[i] = lowerBound;
}
bool nextCombinationExists;
do
{
action(array, indices);
nextCombinationExists = false;
for (int currentDimension = 0; currentDimension < dimensions; currentDimension++)
{
int currentIndex = indices[currentDimension];
int currentUpperBound = array.GetUpperBound(currentDimension);
if (++currentIndex > currentUpperBound)
{
indices[currentDimension] = array.GetLowerBound(currentDimension);
}
else
{
indices[currentDimension] = currentIndex;
nextCombinationExists = true;
break;
}
}
}
while (nextCombinationExists);
}
/// <summary>
/// Get the serialization surrogate for a type, if any, else return null.
/// </summary>
/// <param name="type">The type to check for surrogate.</param>
/// <returns>
/// Returns the associated surrogate, if any, else null.
/// </returns>
protected ISerializationSurrogate GetSurrogateFor(Type type)
{
if (type == null) throw new ArgumentNullException("type");
if (surrogateSelector == null) return null;
ISerializationSurrogate surrogate;
if (!surrogatesByType.TryGetValue(type, out surrogate))
{
ISurrogateSelector selectorFoundInChain;
surrogate = surrogateSelector.GetSurrogate(type, context, out selectorFoundInChain);
surrogatesByType[type] = surrogate;
}
return surrogate;
}
/// <summary>
/// Calls any methods which are marked with a given attribute and accepting a single parameter
/// of type <see cref="StreamingContext"/>.
/// </summary>
/// <param name="value">The object to search for methods.</param>
/// <param name="attributeType">The type of the attribute.</param>
protected void FireSerializationEvent(object value, Type attributeType)
{
if (attributeType == null) throw new ArgumentNullException("attributeType");
if (value == null) return;
var type = value.GetType();
IList<MethodInfo> onSerializingMethods = GetMethodsWithAttribute(type, attributeType);
for (int i = 0; i < onSerializingMethods.Count; i++)
{
var onSerializingMethod = onSerializingMethods[i];
onSerializingMethod.Invoke(value, new object[] { context });
}
}
/// <summary>
/// Returns the methods of a type which are tagged with a given attribute and taking a
/// single parameter of type <see cref="StreamingContext"/>.
/// </summary>
/// <param name="type">The type having the methods.</param>
/// <param name="attributeType">The type of the attribute.</param>
/// <returns>Returns public and non-public methods conforming to the above search criteria.</returns>
protected IList<MethodInfo> GetMethodsWithAttribute(Type type, Type attributeType)
{
if (type == null) throw new ArgumentNullException("type");
if (attributeType == null) throw new ArgumentNullException("attributeType");
var searchKey = new AttributedMethodSearch { HolderType = type, AttributeType = attributeType };
IList<MethodInfo> attributedMethods;
if (!attributedMethodsDictionary.TryGetValue(searchKey, out attributedMethods))
{
var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
attributedMethods = new List<MethodInfo>(methods.Length);
for (int i = 0; i < methods.Length; i++)
{
var method = methods[i];
var attributes = method.GetCustomAttributes(attributeType, true);
if (attributes.Length > 0)
{
var parameters = method.GetParameters();
if (parameters.Length != 1 || parameters[0].ParameterType != typeof(StreamingContext))
{
throw new SerializationException(
String.Format(
"The method '{0}' attributed with '{1}' expects a single SerializationContext parameter.",
method.Name,
attributeType.Name));
}
attributedMethods.Add(method);
}
}
attributedMethodsDictionary.Add(searchKey, attributedMethods);
}
return attributedMethods;
}
/// <summary>
/// Ensures that a type and all its base types are marked with the <see cref="SerializableAttribute"/>.
/// </summary>
/// <param name="type">The type to check.</param>
/// <exception cref="SerializationException">
/// When the type or at least one of its ancestors is not marked as serializable.
/// </exception>
protected void EnsureTypeIsSerializable(Type type)
{
if (type == null) throw new ArgumentNullException("type");
for (; type != null; type = type.BaseType)
{
if (!type.IsEnum && !type.IsInterface && type.GetCustomAttributes(typeof(SerializableAttribute), false).Length == 0)
{
throw new SerializationException(String.Format("The type '{0}' is not marked with SerializableAttribute.", type.FullName));
}
}
}
/// <summary>
/// Get the serializable members of a type via reflection,
/// excluding those marked with <see cref="NonSerializedAttribute"/>.
/// </summary>
/// <param name="type">The type whose members to examine.</param>
protected MemberInfo[] GetSerializableMembersViaReflection(Type type)
{
if (type == null) throw new ArgumentNullException("type");
MemberInfo[] members;
if (!serializableMembersByType.TryGetValue(type, out members))
{
members = FormatterServices.GetSerializableMembers(type, context);
serializableMembersByType[type] = members;
}
return members;
}
#endregion
}
}