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
|
//------------------------------------------------------------------------------
// <copyright file="XmlSerializationGeneratedCode.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Xml.Serialization {
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Threading;
using System.Reflection;
using System.Security;
using System.Globalization;
/// <include file='doc\XmlSerializationGeneratedCode.uex' path='docs/doc[@for="XmlSerializationGeneratedCode"]/*' />
///<internalonly/>
public abstract class XmlSerializationGeneratedCode {
TempAssembly tempAssembly;
int threadCode;
ResolveEventHandler assemblyResolver;
internal void Init(TempAssembly tempAssembly) {
this.tempAssembly = tempAssembly;
// only hook the assembly resolver if we have something to help us do the resolution
if (tempAssembly != null && tempAssembly.NeedAssembyResolve) {
// we save the threadcode to make sure we don't handle any resolve events for any other threads
threadCode = Thread.CurrentThread.GetHashCode();
assemblyResolver = new ResolveEventHandler(OnAssemblyResolve);
AppDomain.CurrentDomain.AssemblyResolve += assemblyResolver;
}
}
// this method must be called at the end of serialization
internal void Dispose() {
if (assemblyResolver != null)
AppDomain.CurrentDomain.AssemblyResolve -= assemblyResolver;
assemblyResolver = null;
}
internal Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) {
if (tempAssembly != null && Thread.CurrentThread.GetHashCode() == threadCode)
return tempAssembly.GetReferencedAssembly(args.Name);
return null;
}
}
internal class XmlSerializationCodeGen {
IndentedWriter writer;
int nextMethodNumber = 0;
Hashtable methodNames = new Hashtable();
ReflectionAwareCodeGen raCodeGen;
TypeScope[] scopes;
TypeDesc stringTypeDesc = null;
TypeDesc qnameTypeDesc = null;
string access;
string className;
TypeMapping[] referencedMethods;
int references = 0;
Hashtable generatedMethods = new Hashtable();
internal XmlSerializationCodeGen(IndentedWriter writer, TypeScope[] scopes, string access, string className) {
this.writer = writer;
this.scopes = scopes;
if (scopes.Length > 0) {
stringTypeDesc = scopes[0].GetTypeDesc(typeof(string));
qnameTypeDesc = scopes[0].GetTypeDesc(typeof(XmlQualifiedName));
}
this.raCodeGen = new ReflectionAwareCodeGen(writer);
this.className = className;
this.access = access;
}
internal IndentedWriter Writer { get { return writer; } }
internal int NextMethodNumber { get { return nextMethodNumber; } set { nextMethodNumber = value; } }
internal ReflectionAwareCodeGen RaCodeGen { get { return raCodeGen; } }
internal TypeDesc StringTypeDesc { get { return stringTypeDesc; } }
internal TypeDesc QnameTypeDesc { get { return qnameTypeDesc; } }
internal string ClassName { get { return className; } }
internal string Access { get { return access; } }
internal TypeScope[] Scopes { get { return scopes; } }
internal Hashtable MethodNames { get { return methodNames; } }
internal Hashtable GeneratedMethods { get { return generatedMethods; } }
internal virtual void GenerateMethod(TypeMapping mapping){}
internal void GenerateReferencedMethods() {
while(references > 0) {
TypeMapping mapping = referencedMethods[--references];
GenerateMethod(mapping);
}
}
internal string ReferenceMapping(TypeMapping mapping) {
if (!mapping.IsSoap) {
if (generatedMethods[mapping] == null) {
referencedMethods = EnsureArrayIndex(referencedMethods, references);
referencedMethods[references++] = mapping;
}
}
return (string)methodNames[mapping];
}
TypeMapping[] EnsureArrayIndex(TypeMapping[] a, int index) {
if (a == null) return new TypeMapping[32];
if (index < a.Length) return a;
TypeMapping[] b = new TypeMapping[a.Length + 32];
Array.Copy(a, b, index);
return b;
}
internal void WriteQuotedCSharpString(string value) {
raCodeGen.WriteQuotedCSharpString(value);
}
internal void GenerateHashtableGetBegin(string privateName, string publicName) {
writer.Write(typeof(Hashtable).FullName);
writer.Write(" ");
writer.Write(privateName);
writer.WriteLine(" = null;");
writer.Write("public override ");
writer.Write(typeof(Hashtable).FullName);
writer.Write(" ");
writer.Write(publicName);
writer.WriteLine(" {");
writer.Indent++;
writer.WriteLine("get {");
writer.Indent++;
writer.Write("if (");
writer.Write(privateName);
writer.WriteLine(" == null) {");
writer.Indent++;
writer.Write(typeof(Hashtable).FullName);
writer.Write(" _tmp = new ");
writer.Write(typeof(Hashtable).FullName);
writer.WriteLine("();");
}
internal void GenerateHashtableGetEnd(string privateName) {
writer.Write("if (");
writer.Write(privateName);
writer.Write(" == null) ");
writer.Write(privateName);
writer.WriteLine(" = _tmp;");
writer.Indent--;
writer.WriteLine("}");
writer.Write("return ");
writer.Write(privateName);
writer.WriteLine(";");
writer.Indent--;
writer.WriteLine("}");
writer.Indent--;
writer.WriteLine("}");
}
internal void GeneratePublicMethods(string privateName, string publicName, string[] methods, XmlMapping[] xmlMappings) {
GenerateHashtableGetBegin(privateName, publicName);
if (methods != null && methods.Length != 0 && xmlMappings != null && xmlMappings.Length == methods.Length) {
for (int i = 0; i < methods.Length; i++) {
if (methods[i] == null)
continue;
writer.Write("_tmp[");
WriteQuotedCSharpString(xmlMappings[i].Key);
writer.Write("] = ");
WriteQuotedCSharpString(methods[i]);
writer.WriteLine(";");
}
}
GenerateHashtableGetEnd(privateName);
}
internal void GenerateSupportedTypes(Type[] types) {
writer.Write("public override ");
writer.Write(typeof(bool).FullName);
writer.Write(" CanSerialize(");
writer.Write(typeof(Type).FullName);
writer.WriteLine(" type) {");
writer.Indent++;
Hashtable uniqueTypes = new Hashtable();
for (int i = 0; i < types.Length; i++) {
Type type = types[i];
if (type == null)
continue;
if (!type.IsPublic && !type.IsNestedPublic)
continue;
if (uniqueTypes[type] != null)
continue;
if (DynamicAssemblies.IsTypeDynamic(type))
continue;
if (type.IsGenericType || type.ContainsGenericParameters && DynamicAssemblies.IsTypeDynamic(type.GetGenericArguments()))
continue;
uniqueTypes[type] = type;
writer.Write("if (type == typeof(");
writer.Write(CodeIdentifier.GetCSharpName(type));
writer.WriteLine(")) return true;");
}
writer.WriteLine("return false;");
writer.Indent--;
writer.WriteLine("}");
}
internal string GenerateBaseSerializer(string baseSerializer, string readerClass, string writerClass, CodeIdentifiers classes) {
baseSerializer = CodeIdentifier.MakeValid(baseSerializer);
baseSerializer = classes.AddUnique(baseSerializer, baseSerializer);
writer.WriteLine();
writer.Write("public abstract class ");
writer.Write(CodeIdentifier.GetCSharpName(baseSerializer));
writer.Write(" : ");
writer.Write(typeof(XmlSerializer).FullName);
writer.WriteLine(" {");
writer.Indent++;
writer.Write("protected override ");
writer.Write(typeof(XmlSerializationReader).FullName);
writer.WriteLine(" CreateReader() {");
writer.Indent++;
writer.Write("return new ");
writer.Write(readerClass);
writer.WriteLine("();");
writer.Indent--;
writer.WriteLine("}");
writer.Write("protected override ");
writer.Write(typeof(XmlSerializationWriter).FullName);
writer.WriteLine(" CreateWriter() {");
writer.Indent++;
writer.Write("return new ");
writer.Write(writerClass);
writer.WriteLine("();");
writer.Indent--;
writer.WriteLine("}");
writer.Indent--;
writer.WriteLine("}");
return baseSerializer;
}
internal string GenerateTypedSerializer(string readMethod, string writeMethod, XmlMapping mapping, CodeIdentifiers classes, string baseSerializer, string readerClass, string writerClass) {
string serializerName = CodeIdentifier.MakeValid(Accessor.UnescapeName(mapping.Accessor.Mapping.TypeDesc.Name));
serializerName = classes.AddUnique(serializerName + "Serializer", mapping);
writer.WriteLine();
writer.Write("public sealed class ");
writer.Write(CodeIdentifier.GetCSharpName(serializerName));
writer.Write(" : ");
writer.Write(baseSerializer);
writer.WriteLine(" {");
writer.Indent++;
writer.WriteLine();
writer.Write("public override ");
writer.Write(typeof(bool).FullName);
writer.Write(" CanDeserialize(");
writer.Write(typeof(XmlReader).FullName);
writer.WriteLine(" xmlReader) {");
writer.Indent++;
if (mapping.Accessor.Any) {
writer.WriteLine("return true;");
}
else {
writer.Write("return xmlReader.IsStartElement(");
WriteQuotedCSharpString(mapping.Accessor.Name);
writer.Write(", ");
WriteQuotedCSharpString(mapping.Accessor.Namespace);
writer.WriteLine(");");
}
writer.Indent--;
writer.WriteLine("}");
if (writeMethod != null) {
writer.WriteLine();
writer.Write("protected override void Serialize(object objectToSerialize, ");
writer.Write(typeof(XmlSerializationWriter).FullName);
writer.WriteLine(" writer) {");
writer.Indent++;
writer.Write("((");
writer.Write(writerClass);
writer.Write(")writer).");
writer.Write(writeMethod);
writer.Write("(");
if (mapping is XmlMembersMapping) {
writer.Write("(object[])");
}
writer.WriteLine("objectToSerialize);");
writer.Indent--;
writer.WriteLine("}");
}
if (readMethod != null) {
writer.WriteLine();
writer.Write("protected override object Deserialize(");
writer.Write(typeof(XmlSerializationReader).FullName);
writer.WriteLine(" reader) {");
writer.Indent++;
writer.Write("return ((");
writer.Write(readerClass);
writer.Write(")reader).");
writer.Write(readMethod);
writer.WriteLine("();");
writer.Indent--;
writer.WriteLine("}");
}
writer.Indent--;
writer.WriteLine("}");
return serializerName;
}
void GenerateTypedSerializers(Hashtable serializers) {
string privateName = "typedSerializers";
GenerateHashtableGetBegin(privateName, "TypedSerializers");
foreach (string key in serializers.Keys) {
writer.Write("_tmp.Add(");
WriteQuotedCSharpString(key);
writer.Write(", new ");
writer.Write((string)serializers[key]);
writer.WriteLine("());");
}
GenerateHashtableGetEnd("typedSerializers");
}
//GenerateGetSerializer(serializers, xmlMappings);
void GenerateGetSerializer(Hashtable serializers, XmlMapping[] xmlMappings) {
writer.Write("public override ");
writer.Write(typeof(XmlSerializer).FullName);
writer.Write(" GetSerializer(");
writer.Write(typeof(Type).FullName);
writer.WriteLine(" type) {");
writer.Indent++;
for (int i = 0; i < xmlMappings.Length; i++) {
if (xmlMappings[i] is XmlTypeMapping) {
Type type = xmlMappings[i].Accessor.Mapping.TypeDesc.Type;
if (type == null)
continue;
if (!type.IsPublic && !type.IsNestedPublic)
continue;
if (DynamicAssemblies.IsTypeDynamic(type))
continue;
if (type.IsGenericType || type.ContainsGenericParameters && DynamicAssemblies.IsTypeDynamic(type.GetGenericArguments()))
continue;
writer.Write("if (type == typeof(");
writer.Write(CodeIdentifier.GetCSharpName(type));
writer.Write(")) return new ");
writer.Write((string)serializers[xmlMappings[i].Key]);
writer.WriteLine("();");
}
}
writer.WriteLine("return null;");
writer.Indent--;
writer.WriteLine("}");
}
internal void GenerateSerializerContract(string className, XmlMapping[] xmlMappings, Type[] types, string readerType, string[] readMethods, string writerType, string[] writerMethods, Hashtable serializers) {
writer.WriteLine();
writer.Write("public class XmlSerializerContract : global::");
writer.Write(typeof(XmlSerializerImplementation).FullName);
writer.WriteLine(" {");
writer.Indent++;
writer.Write("public override global::");
writer.Write(typeof(XmlSerializationReader).FullName);
writer.Write(" Reader { get { return new ");
writer.Write(readerType);
writer.WriteLine("(); } }");
writer.Write("public override global::");
writer.Write(typeof(XmlSerializationWriter).FullName);
writer.Write(" Writer { get { return new ");
writer.Write(writerType);
writer.WriteLine("(); } }");
GeneratePublicMethods("readMethods", "ReadMethods", readMethods, xmlMappings);
GeneratePublicMethods("writeMethods", "WriteMethods", writerMethods, xmlMappings);
GenerateTypedSerializers(serializers);
GenerateSupportedTypes(types);
GenerateGetSerializer(serializers, xmlMappings);
writer.Indent--;
writer.WriteLine("}");
}
internal static bool IsWildcard(SpecialMapping mapping) {
if (mapping is SerializableMapping)
return ((SerializableMapping)mapping).IsAny;
return mapping.TypeDesc.CanBeElementValue;
}
}
}
|