forked from JoshClose/CsvHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
459 lines (375 loc) · 11.4 KB
/
Extensions.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
using CsvHelper.DocsGenerator.Formatters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
namespace CsvHelper.DocsGenerator
{
public static class Extensions
{
// Assembly
public static string GetHtmlName(Assembly assembly)
{
throw new NotImplementedException();
}
// Type
public static string GetTypeName(this Type type)
{
if (type.IsEnum)
{
return "Enum";
}
if (type.IsInterface)
{
return "Interface";
}
if (type.IsClass)
{
return "Class";
}
throw new InvalidOperationException($"No type name found for type '{type.GetFullName()}'.");
}
public static string GetHtmlName(this Type type)
{
return HtmlFormat(type);
}
public static string GetCodeName(this Type type)
{
return HtmlFormat(type, isCodeBlock: true);
}
public static string GetFullName(this Type type)
{
return $"{type.Namespace}.{type.Name}";
}
public static string GetFullHtmlName(this Type type)
{
return $"{type.Namespace}.{type.GetHtmlName()}";
}
public static string GetFullCodeName(this Type type)
{
return $"{type.Namespace}.{type.GetCodeName()}";
}
public static string GetSummary(this Type type)
{
return GetSummary($"T:{type.GetFullName()}");
}
// Property
public static string GetHtmlName(this PropertyInfo property)
{
return property.Name;
}
public static string GetCodeName(this PropertyInfo property)
{
throw new NotImplementedException();
}
public static string GetFullName(this PropertyInfo property)
{
return $"{property.DeclaringType.FullName}.{property.Name}";
}
public static string GetFullHtmlName(this PropertyInfo property)
{
throw new NotImplementedException();
}
public static string GetFullCodeName(this PropertyInfo property)
{
throw new NotImplementedException();
}
public static string GetSummary(this PropertyInfo property)
{
var parameters = property.GetIndexParameters().ToList();
var parametersText = string.Empty;
if (parameters.Count > 0)
{
parametersText = $"({string.Join(",", parameters.Select(p => p.ParameterType.FullName))})";
}
return GetSummary($"P:{property.GetFullName()}{parametersText}");
}
// Field
public static string GetHtmlName(this FieldInfo field)
{
return field.Name;
}
public static string GetCodeName(this FieldInfo field)
{
return field.Name;
}
public static string GetFullName(this FieldInfo field)
{
return $"{field.DeclaringType.FullName}.{field.Name}";
}
public static string GetFullHtmlName(this FieldInfo field)
{
throw new NotImplementedException();
}
public static string GetFullCodeName(this FieldInfo field)
{
throw new NotImplementedException();
}
public static string GetSummary(this FieldInfo field)
{
return GetSummary($"F:{field.GetFullName()}");
}
// Constructor
public static string GetHtmlName(this ConstructorInfo constructor)
{
return HtmlFormat(constructor);
}
public static string GetCodeName(this ConstructorInfo constructor)
{
throw new NotImplementedException();
}
public static string GetFullName(this ConstructorInfo constructor)
{
throw new NotImplementedException();
}
public static string GetFullHtmlName(this ConstructorInfo constructor)
{
throw new NotImplementedException();
}
public static string GetFullCodeName(this ConstructorInfo constructor)
{
throw new NotImplementedException();
}
public static string GetSummary(this ConstructorInfo constructor)
{
return GetSummary($"M:{XmlDocFormat(constructor)}");
}
// Method
public static string GetHtmlName(this MethodInfo method)
{
return HtmlFormat(method);
}
public static string GetCodeName(this MethodInfo method)
{
throw new NotImplementedException();
}
public static string GetFullName(this MethodInfo method)
{
throw new NotImplementedException();
}
public static string GetFullHtmlName(this MethodInfo method)
{
throw new NotImplementedException();
}
public static string GetFullCodeName(this MethodInfo method)
{
throw new NotImplementedException();
}
public static string GetSummary(this MethodInfo method)
{
return GetSummary($"M:{XmlDocFormat(method)}");
}
// Private
private static Type GetType(string typeName)
{
Type type = null;
if (typeName.StartsWith("CsvHelper"))
{
var assembly = Assembly.GetAssembly(typeof(CsvHelperException));
type = assembly.GetType(typeName);
var currentTypeName = typeName;
while (type == null && !string.IsNullOrWhiteSpace(currentTypeName) && currentTypeName.Contains('.'))
{
currentTypeName = currentTypeName.Substring(0, currentTypeName.LastIndexOf('.'));
type = assembly.GetType(typeName);
}
}
else
{
type = Type.GetType(typeName);
}
return type;
}
private static string GetSummary(string memberName)
{
var members = XmlDocs.XElement.Descendants("member");
var member = members?.SingleOrDefault(m => m.Attribute("name")?.Value == memberName);
var summary = member?.Element("summary");
if (summary != null)
{
var summaryText = new List<string>();
foreach (var node in summary.Nodes())
{
string text;
if (node.NodeType == XmlNodeType.Element)
{
string typeName = string.Empty;
var el = (XElement)node;
switch (el.Name.ToString())
{
case "paramref":
typeName = el.Attribute("name").Value;
break;
case "see":
typeName = el.Attribute("cref").Value.Substring(2);
break;
case "c":
typeName = el.Value;
break;
default:
throw new InvalidOperationException($"Unhandled element '{el.Name}'.");
}
var type = GetType(typeName);
text = type == null ? typeName : type.GetFullCodeName();
text = $"``{text.Trim()}``";
}
else if (node.NodeType == XmlNodeType.Text)
{
text = node.ToString();
}
else
{
throw new InvalidOperationException($"Unhandled node type '{node.NodeType}'.");
}
// Replace multiple spaces with a single space.
text = Regex.Replace(text, @"\s{2,}", " ").Trim();
summaryText.Add(text);
}
return string.Join(" ", summaryText);
}
if (memberName.Substring(2).StartsWith("CsvHelper"))
{
Console.WriteLine($"No summary found for '{memberName}'.");
}
return null;
}
private static string HtmlFormat(Type type, bool generateLinks = false, bool isCodeBlock = false)
{
var symbols = isCodeBlock ? Symbols.Code : Symbols.Html;
var @namespace = type.Namespace;
var name = type.Name;
if (type.IsByRef)
{
name = name.TrimEnd('&');
}
if (generateLinks)
{
if (@namespace.StartsWith("CsvHelper"))
{
name = $"[{name}](/api/{@namespace}/{name})";
}
else
{
name = $"[{name}](https://docs.microsoft.com/en-us/dotnet/api/{@namespace.ToLower()}.{name.ToLower()})";
}
}
var genericArgumentsText = string.Empty;
var genericArguments = type.GetGenericArguments().ToList();
if (genericArguments.Count > 0)
{
name = name.Substring(0, name.IndexOf('`'));
genericArgumentsText = $"{symbols["<"]}{string.Join(", ", genericArguments.Select(a => HtmlFormat(a)))}{symbols[">"]}";
}
return $"{name}{genericArgumentsText}";
}
private static string HtmlFormat(MethodBase methodInfo, bool generateLinks = false, bool isCodeBlock = false)
{
var symbols = isCodeBlock ? Symbols.Code : Symbols.Html;
var @namespace = methodInfo.DeclaringType.Namespace;
var typeName = methodInfo.DeclaringType.Name;
var methodName = methodInfo.Name;
var name = methodName;
if (methodInfo.IsConstructor)
{
name = methodInfo.DeclaringType.IsGenericType
? typeName.Substring(0, typeName.IndexOf('`'))
: typeName;
}
var genericArgumentsText = string.Empty;
var genericArguments = new List<Type>();
if (!methodInfo.IsConstructor)
{
genericArguments = methodInfo.GetGenericArguments().ToList();
if (genericArguments.Count > 0)
{
genericArgumentsText = $"{symbols["<"]}{string.Join(", ", genericArguments.Select(a => HtmlFormat(a)))}{symbols[">"]}";
}
}
var parametersText = string.Empty;
var parameters = methodInfo.GetParameters().ToList();
if (parameters.Count > 0)
{
var typeGenericArguments = methodInfo.DeclaringType.GetGenericArguments();
parametersText = string.Join(", ", parameters.Select(p =>
{
// Don't generate links if the type is a generic parameter.
var shouldGenerateLinks = generateLinks &&
!(
typeGenericArguments.Any(a => $"{a.Namespace}.{a.Name}" == $"{p.ParameterType.Namespace}.{p.ParameterType.Name}") ||
genericArguments.Any(a => $"{a.Namespace}.{a.Name}" == $"{p.ParameterType.Namespace}.{p.ParameterType.Name}")
);
var outText = p.IsOut ? "out " : string.Empty;
return $"{outText}{HtmlFormat(p.ParameterType, shouldGenerateLinks)}";
}));
}
return $"{name}{genericArgumentsText}({parametersText})";
}
private static string XmlDocFormat(Type type)
{
var @namespace = type.Namespace;
var name = type.Name;
return $"{@namespace}.{name}";
}
private static string XmlDocFormat(MethodBase methodInfo)
{
var typeText = XmlDocFormat(methodInfo.DeclaringType);
var methodName = methodInfo.Name.Replace('.', '#');
var typeGenericArguments = methodInfo.DeclaringType.GetGenericArguments().ToList();
var methodGenericArguments = new List<Type>();
if (!methodInfo.IsConstructor)
{
methodGenericArguments = methodInfo.GetGenericArguments().ToList();
if (methodGenericArguments.Count > 0)
{
methodName = $"{methodName}``{methodGenericArguments.Count}";
}
}
var parametersText = string.Empty;
var parameters = methodInfo.GetParameters().ToList();
if (parameters.Count > 0)
{
parametersText = $"({string.Join(",", parameters.Select(p => XmlDocFormat(p.ParameterType, typeGenericArguments, methodGenericArguments)))})";
}
return $"{typeText}.{methodName}{parametersText}";
}
private static string XmlDocFormat(Type parameterType, List<Type> typeGenericParameters, List<Type> methodGenericParameters)
{
var @namespace = parameterType.Namespace;
var name = parameterType.Name;
if (parameterType.IsByRef)
{
name = name.TrimEnd('&');
}
var typeName = $"{@namespace}.{name}";
// Check if the parameter is a generic argument of a type.
var index = typeGenericParameters.FindIndex(t => $"{t.Namespace}.{t.Name}" == typeName);
if (index >= 0)
{
var refText = parameterType.IsByRef ? "@" : string.Empty;
return $"`{index}{refText}";
}
// Check if the parameter is a generic argument of a method.
index = methodGenericParameters.FindIndex(t => $"{t.Namespace}.{t.Name}" == typeName);
if (index >= 0)
{
var refText = parameterType.IsByRef ? "@" : string.Empty;
return $"``{index}{refText}";
}
var genericArgumentsText = string.Empty;
var genericArguments = parameterType.GetGenericArguments().ToList();
if (genericArguments.Count > 0)
{
name = name.Substring(0, name.IndexOf('`'));
genericArgumentsText = $"{{{string.Join(",", genericArguments.Select(a => XmlDocFormat(a, typeGenericParameters, methodGenericParameters)))}}}";
}
if (parameterType.IsByRef)
{
name += "@";
}
return $"{@namespace}.{name}{genericArgumentsText}";
}
}
}