-
Notifications
You must be signed in to change notification settings - Fork 1
/
TypeHelper.cs
429 lines (381 loc) · 15.7 KB
/
TypeHelper.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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TypeHelper.cs" company="7HillApps">
// Copyright 2011 7HillApps
// </copyright>
//
// <summary>
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace SevenHillApps.Csharp2Xsd
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Schema;
/// <summary>
/// Generate XML for CLR primitives
/// </summary>
public static class Primitive2Xml
{
/// <summary>
/// List of CLR / XSD types
/// </summary>
private static readonly Lazy<Dictionary<Type, XmlQualifiedName>> ClrType2Xml =
// ReSharper restore InconsistentNaming
new Lazy<Dictionary<Type, XmlQualifiedName>>(
() =>
{
var d = new Dictionary<Type, XmlQualifiedName>();
d[typeof(string)] = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
d[typeof(String)] = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
d[typeof(int)] = new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema");
d[typeof(Int32)] = new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema");
d[typeof(bool)] = new XmlQualifiedName("boolean", "http://www.w3.org/2001/XMLSchema");
d[typeof(Boolean)] = new XmlQualifiedName("boolean", "http://www.w3.org/2001/XMLSchema");
d[typeof(float)] = new XmlQualifiedName("float", "http://www.w3.org/2001/XMLSchema");
d[typeof(double)] = new XmlQualifiedName("double", "http://www.w3.org/2001/XMLSchema");
d[typeof(Double)] = new XmlQualifiedName("double", "http://www.w3.org/2001/XMLSchema");
d[typeof(DateTime)] = new XmlQualifiedName("dateTime", "http://www.w3.org/2001/XMLSchema");
return d;
});
/// <summary>
/// extension method to return Xml QN for type
/// </summary>
/// <param name="t">The t.</param>
/// <returns>Xml QN</returns>
public static XmlQualifiedName XmlName(this Type t)
{
return t != null && ClrType2Xml.Value.ContainsKey(t) ? ClrType2Xml.Value[t] : null;
}
/// <summary>
/// XMLs the name.
/// </summary>
/// <param name="n3">The n3.</param>
/// <returns>XML QN</returns>
public static XmlQualifiedName XmlName(string n3)
{
var t = Type.GetType(n3);
return t.XmlName();
}
}
/// <summary>
/// Type helpers for XML
/// </summary>
public static class Type2XmlHelper
{
/// <summary>
/// Split type string
/// </summary>
/// <param name="s">The s.</param>
/// <param name="shortVersion">if set to <c>true</c> [short version].</param>
/// <returns>type name</returns>
public static string InternalType(string s, bool shortVersion)
{
var n = s.Split(new[] { '[', ',' });
if (!shortVersion)
{
return n[2];
}
var n2 = n[2].Split(new[] { '.' });
return n2[n2.Length - 1];
}
/// <summary>
/// Gets the custom attribute data.
/// </summary>
/// <param name="attribList">A list.</param>
/// <param name="attrib">The attrib.</param>
/// <returns>attrib data</returns>
public static CustomAttributeData GetCustomAttributeData(IList<CustomAttributeData> attribList, Type attrib)
{
return attribList == null
? null
: (from a in attribList
let t1 = a.Constructor.ReflectedType
where t1.FullName == attrib.FullName
select a).FirstOrDefault();
}
/// <summary>
/// Gets the type of the custom attribute data of. -- hillarious
/// </summary>
/// <param name="t">The t.</param>
/// <param name="attrib">The attrib.</param>
/// <returns>attrib data</returns>
public static CustomAttributeData GetCustomAttributeDataOfType(this Type t, Type attrib)
{
return GetCustomAttributeData(t.GetCustomAttributesData(), attrib);
}
/// <summary>
/// Gets the type of the custom attribute data of.
/// </summary>
/// <param name="p">The p.</param>
/// <param name="attrib">The attrib.</param>
/// <returns>attrib data</returns>
public static CustomAttributeData GetCustomAttributeDataOfType(this PropertyInfo p, Type attrib)
{
return GetCustomAttributeData(p.GetCustomAttributesData(), attrib);
}
/// <summary>
/// Schemas the key. Not used currently
/// </summary>
/// <returns>key a </returns>
public static XmlSchemaKey SchemaKey()
{
var elementKey = new XmlSchemaKey
{
Name = "AccountKey",
Selector = new XmlSchemaXPath
{
XPath = "r:parts/r:part"
}
};
{
var field = new XmlSchemaXPath
{
XPath = "@number"
};
elementKey.Fields.Add(field);
}
return elementKey;
}
/// <summary>
/// Gets the enum schema.
/// </summary>
/// <param name="t">The t.</param>
/// <returns>schema info</returns>
public static SchemaInfo GetEnumSchema(this Type t)
{
if (!t.IsEnum)
{
return null;
}
var schemaInfo = new SchemaInfo();
var classType = new XmlSchemaSimpleType
{
Name = t.Name
};
var attribData = t.GetCustomAttributeDataOfType(typeof(DataContractAttribute));
if (attribData.NamedArguments != null && attribData.NamedArguments.Count > 0)
{
foreach (var p1 in attribData.NamedArguments)
{
switch (p1.MemberInfo.Name)
{
case "Namespace":
schemaInfo.Schema.TargetNamespace = p1.TypedValue.Value as string;
break;
}
}
}
var content = new XmlSchemaSimpleTypeRestriction();
classType.Content = content;
content.BaseTypeName = typeof(string).XmlName();
foreach (var e in t.GetEnumNames())
{
content.Facets.Add(new XmlSchemaEnumerationFacet
{
Value = e
});
}
schemaInfo.Schema.Items.Add(classType);
return schemaInfo;
}
/// <summary>
/// Gets the class schema.
/// </summary>
/// <param name="t">The t.</param>
/// <returns>schema info</returns>
public static SchemaInfo GetClassSchema(this Type t)
{
var schemaInfo = new SchemaInfo();
var classType = new XmlSchemaComplexType
{
Name = t.Name
};
var attribData = t.GetCustomAttributeDataOfType(typeof(DataContractAttribute));
if (attribData.NamedArguments != null && attribData.NamedArguments.Count > 0)
{
foreach (var p1 in attribData.NamedArguments)
{
switch (p1.MemberInfo.Name)
{
case "Namespace":
schemaInfo.Schema.TargetNamespace = p1.TypedValue.Value as string;
break;
}
}
}
var sequence = new XmlSchemaSequence();
classType.Particle = sequence;
var propList = t.GetProperties();
foreach (var p1 in propList)
{
var el = new XmlSchemaElement
{
Name = p1.Name,
MinOccurs = 0
};
var xmlName = p1.PropertyType.XmlName();
if (xmlName != null)
{
el.SchemaTypeName = xmlName;
}
else
{
if (p1.PropertyType.IsListType())
{
// what is this a list of?
if (p1.PropertyType.FullName != null)
{
var pr = Primitive2Xml.XmlName(InternalType(p1.PropertyType.FullName, false));
if (pr != null)
{
el.SchemaTypeName = new XmlQualifiedName("ArrayOf" + pr.Name);
schemaInfo.CustomTypes.Add(el.SchemaTypeName);
}
else
{
var schName = InternalType(p1.PropertyType.FullName, true);
el.SchemaTypeName = new XmlQualifiedName("ArrayOf" + schName);
schemaInfo.CustomTypes.Add(new XmlQualifiedName(schName));
schemaInfo.CustomTypes.Add(el.SchemaTypeName);
}
}
}
else if (p1.PropertyType.Name.Contains("Nullable"))
{
// what is this a nullable of?
if (p1.PropertyType.FullName != null)
{
var pr = Primitive2Xml.XmlName(InternalType(p1.PropertyType.FullName, false));
if (pr != null)
{
el.SchemaTypeName = pr;
}
else
{
var schName = InternalType(p1.PropertyType.FullName, true);
el.SchemaTypeName = new XmlQualifiedName(schName);
schemaInfo.CustomTypes.Add(el.SchemaTypeName);
}
el.IsNillable = true;
}
}
else
{
el.SchemaTypeName = new XmlQualifiedName(p1.PropertyType.Name);
schemaInfo.CustomTypes.Add(el.SchemaTypeName);
}
}
// get data member
var attrData = p1.GetCustomAttributeDataOfType(typeof(DataMemberAttribute));
if (attrData != null && attrData.NamedArguments != null
&& attrData.NamedArguments.Count > 0)
{
foreach (var pp in attrData.NamedArguments)
{
switch (pp.MemberInfo.Name)
{
case "IsRequired":
{
var v = (bool)pp.TypedValue.Value;
el.MinOccurs = v ? 1 : 0;
}
break;
}
}
}
sequence.Items.Add(el);
}
schemaInfo.Schema.Items.Add(classType);
return schemaInfo;
}
/// <summary>
/// Gets the list schema.
/// </summary>
/// <param name="t">The t.</param>
/// <returns>schema info</returns>
public static SchemaInfo GetListSchema(this Type t)
{
var schemaInfo = new SchemaInfo();
var classType = new XmlSchemaComplexType
{
Name = "ArrayOf" + t.Name
};
var sequence = new XmlSchemaSequence();
schemaInfo.Schema.Items.Add(classType);
classType.Particle = sequence;
sequence.Items.Add(new XmlSchemaElement
{
Name = t.Name,
MinOccurs = 0,
MaxOccursString = "unbounded",
SchemaTypeName = new XmlQualifiedName(t.Name)
});
return schemaInfo;
}
/// <summary>
/// Gets the schema.
/// </summary>
/// <param name="t">The t.</param>
/// <param name="isList">if set to <c>true</c> [is list].</param>
/// <returns>the schema it is</returns>
public static SchemaInfo GetSchema(this Type t, bool isList)
{
if (isList)
{
return t.GetListSchema();
}
if (t.IsClass)
{
return t.GetClassSchema();
}
return t.IsEnum ? t.GetEnumSchema() : null;
}
/// <summary>
/// Gets the schema.
/// </summary>
/// <param name="t">The t.</param>
/// <returns>schema, am i?</returns>
public static SchemaInfo GetSchema(this Type t)
{
return t.GetSchema(false);
}
/// <summary>
/// Determines whether [is list type] [the specified t].
/// </summary>
/// <param name="t">The t.</param>
/// <returns>
/// <c>true</c> if [is list type] [the specified t]; otherwise, <c>false</c>.
/// </returns>
private static bool IsListType(this Type t)
{
return t.Name == typeof(List<>).Name || t.Name == typeof(ObservableCollection<>).Name
|| t.Name == typeof(IList<>).Name || t.Name == typeof(ICollection<>).Name;
}
#region Nested type: SchemaInfo
public class SchemaInfo
{
public SchemaInfo()
{
Schema = new XmlSchema();
CustomTypes = new List<XmlQualifiedName>();
}
public XmlSchema Schema { get; set; }
public List<XmlQualifiedName> CustomTypes { get; set; }
}
#endregion
}
}