-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDotNetMemberExtensions.cs
More file actions
372 lines (306 loc) · 18.8 KB
/
DotNetMemberExtensions.cs
File metadata and controls
372 lines (306 loc) · 18.8 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
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
//*****************************************************************************
//* Code Factory SDK
//* Copyright (c) 2020 CodeFactory, LLC
//*****************************************************************************
using System;
using System.Text;
using CodeFactory.DotNet.CSharp.FormattedSyntax;
namespace CodeFactory.DotNet
{
/// <summary>
/// Extension methods that support model that implement the <see cref="IDotNetMember"/> interface.
/// </summary>
public static class DotNetMemberExtensions
{
/// <summary>
/// Gets the hash code for a formatted model signature using the C# format.
/// </summary>
/// <param name="source">The sources <see cref="IDotNetModel"/> model.</param>
/// <param name="comparisonType">The type of comparision format to use when generating the hashcode. Default is set to the base comparision type.</param>
/// <returns>The has code of the formatted model.</returns>
/// <exception cref="ArgumentNullException">This is thrown if the model is null.</exception>
public static int FormatCSharpMemberComparisonHashCode(this IDotNetMember source,
MemberComparisonType comparisonType = MemberComparisonType.Base)
{
if (source == null) throw new ArgumentNullException(nameof(source));
int result = 0;
bool includeSecurity = false;
bool includeAttributes = false;
bool includeKeywords = false;
switch (comparisonType)
{
case MemberComparisonType.Security:
includeSecurity = true;
break;
case MemberComparisonType.Full:
includeSecurity = true;
includeKeywords = true;
break;
}
switch (source.MemberType)
{
case DotNetMemberType.Event:
var memberEvent = source as IDotNetEvent;
result = memberEvent.FormatCSharpComparisonHashCode(includeSecurity, includeAttributes,
includeKeywords);
break;
case DotNetMemberType.Field:
var memberField = source as IDotNetField;
result = memberField.FormatCSharpComparisonHashCode(includeSecurity, includeAttributes,
includeKeywords);
break;
case DotNetMemberType.Method:
var memberMethod = source as IDotNetMethod;
result = memberMethod.FormatCSharpComparisonHashCode(includeSecurity, includeAttributes,
includeKeywords);
break;
case DotNetMemberType.Property:
var memberProperty = source as IDotNetProperty;
result = memberProperty.FormatCSharpComparisonHashCode(includeSecurity, includeAttributes,
includeKeywords);
break;
default:
result = 0;
break;
}
return result;
}
/// <summary>
/// Generates the syntax definition of field in c# syntax. The default definition with all options turned off will return the filed signature and constants if defined and the default values.
/// </summary>
/// <param name="source">The source <see cref="IDotNetField"/> model to generate.</param>
/// <param name="includeSecurity">Includes the security scope which the field was defined in the model.</param>
/// <param name="includeAttributes">Includes definition of the attributes assigned to the model.</param>
/// <param name="includeKeywords">Includes all keywords assigned to the field from the source model.</param>
/// <returns>Fully formatted field definition or null if the field data could not be generated.</returns>
public static string FormatCSharpDeclarationSyntax(this IDotNetField source, bool includeSecurity = true,
bool includeAttributes = true, bool includeKeywords = true)
{
if (source == null) return null;
StringBuilder fieldFormatting = new StringBuilder();
if (includeAttributes & source.HasAttributes)
{
foreach (var sourceAttribute in source.Attributes)
{
fieldFormatting.AppendLine(sourceAttribute.FormatCSharpAttributeSignatureSyntax());
}
}
if (includeSecurity)
{
fieldFormatting.Append($"{source.Security.FormatCSharpSyntax()} ");
}
if (includeKeywords)
{
if (source.IsStatic) fieldFormatting.Append($"{Keywords.Static} ");
if (source.IsReadOnly) fieldFormatting.Append($"{Keywords.Readonly} ");
}
if (source.IsConstant) fieldFormatting.Append($"{Keywords.Constant} ");
fieldFormatting.Append($"{source.DataType.FormatCSharpFullTypeName()} ");
fieldFormatting.Append($"{source.DataType.Name}");
if (source.IsConstant)
fieldFormatting.Append($" = {source.DataType.FormatCSharpValueSyntax(source.ConstantValue)}");
return fieldFormatting.ToString();
}
/// <summary>
/// Gets the hash code for a formatted field signature using the C# format.
/// </summary>
/// <param name="source">The sources <see cref="IDotNetField"/> model.</param>
/// <param name="includeSecurity">Optional parameter that determines to generate security in the definition. By default this is false.</param>
/// <param name="includeAttributes">Optional parameter that determines if the attributes should be included in the definition. By default this is false.</param>
/// <param name="includeKeywords">Optional parameter that determines if all keywords other then constant are included in the definition. By default this is false.</param>
/// <returns>The has code of the formatted field.</returns>
/// <exception cref="ArgumentNullException">This is thrown if the model is null.</exception>
public static int FormatCSharpComparisonHashCode(this IDotNetField source, bool includeSecurity = false,
bool includeAttributes = false, bool includeKeywords = false)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return source.FormatCSharpDeclarationSyntax(includeSecurity, includeAttributes, includeKeywords).GetHashCode();
}
/// <summary>
/// Generates the syntax definition of a default no backing fields property definition in c# syntax.
/// </summary>
/// <param name="source">The source <see cref="IDotNetProperty"/> model to generate.</param>
/// <param name="includeSecurity">Includes the security scope which the property was defined in the model.</param>
/// <param name="includeAttributes">Includes definition of the attributes assigned to the model.</param>
/// <param name="includeKeywords">Includes all keywords assigned to the property from the source model.</param>
/// <param name="includeAbstractKeyword">Will include the definition for the abstract keyword in the definition if it is defined. default is false.</param>
/// <returns>Fully formatted property definition or null if the property data could not be generated.</returns>
public static string FormatCSharpDeclarationSyntax(this IDotNetProperty source, bool includeSecurity = true,
bool includeAttributes = true, bool includeKeywords = true,bool includeAbstractKeyword = false)
{
if (source == null) return null;
StringBuilder propertyFormatting = new StringBuilder();
if (includeAttributes & source.HasAttributes)
{
foreach (var sourceAttribute in source.Attributes)
{
propertyFormatting.AppendLine(sourceAttribute.FormatCSharpAttributeSignatureSyntax());
}
}
if (includeSecurity)
{
propertyFormatting.Append($"{source.Security.FormatCSharpSyntax()} ");
}
if (includeKeywords)
{
if (source.IsStatic) propertyFormatting.Append($"{Keywords.Static} ");
if (source.IsSealed) propertyFormatting.Append($"{Keywords.Sealed} ");
if (includeAbstractKeyword & source.IsAbstract) propertyFormatting.Append($"{Keywords.Abstract} ");
if (source.IsOverride) propertyFormatting.Append($"{Keywords.Override} ");
if (source.IsVirtual) propertyFormatting.Append($"{Keywords.Virtual} ");
}
propertyFormatting.Append($"{source.PropertyType.FormatCSharpFullTypeName()} ");
propertyFormatting.Append($"{source.Name} {Symbols.MultipleStatementStart} ");
if (source.HasGet)
{
if (includeSecurity)
{
if (source.Security != source.GetSecurity)
propertyFormatting.Append($"{source.GetSecurity.FormatCSharpSyntax()} ");
}
propertyFormatting.Append("get; ");
}
if (source.HasSet)
{
if (includeSecurity)
{
if (source.Security != source.SetSecurity)
propertyFormatting.Append($"{source.SetSecurity.FormatCSharpSyntax()} ");
}
propertyFormatting.Append("set; ");
}
propertyFormatting.Append(Symbols.MultipleStatementEnd);
return propertyFormatting.ToString();
}
/// <summary>
/// Gets the hash code for a formatted property signature using the C# format.
/// </summary>
/// <param name="source">The sources <see cref="IDotNetProperty"/> model.</param>
/// <param name="includeSecurity">Optional parameter that determines to generate security in the definition. By default this is false.</param>
/// <param name="includeAttributes">Optional parameter that determines if the attributes should be included in the definition. By default this is false.</param>
/// <param name="includeKeywords">Optional parameter that determines if all keywords are included in the definition. By default this is false.</param>
/// <returns>The hash code of the formatted model.</returns>
/// <exception cref="ArgumentNullException">This is thrown if the model is null.</exception>
public static int FormatCSharpComparisonHashCode(this IDotNetProperty source, bool includeSecurity = false,
bool includeAttributes = false, bool includeKeywords = false)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return source.FormatCSharpDeclarationSyntax(includeSecurity, includeAttributes, includeKeywords).GetHashCode();
}
/// <summary>
/// Generates the syntax definition of an event in c# syntax.
/// </summary>
/// <param name="source">The source <see cref="IDotNetEvent"/> model to generate.</param>
/// <param name="includeSecurity">Includes the security scope which was defined in the model.</param>
/// <param name="includeAttributes">Includes definition of the attributes assigned to the model.</param>
/// <param name="includeKeywords">Includes all keywords assigned to the source model.</param>
/// <param name="includeAbstractKeyword">Will include the definition for the abstract keyword in the definition if it is defined. default is false.</param>
/// <returns>Fully formatted event definition or null if the event data could not be generated.</returns>
public static string FormatCSharpDeclarationSyntax(this IDotNetEvent source, bool includeSecurity = true,
bool includeAttributes = true, bool includeKeywords = true, bool includeAbstractKeyword = false)
{
if (source == null) return null;
StringBuilder eventFormatting = new StringBuilder();
if (includeAttributes & source.HasAttributes)
{
foreach (var sourceAttribute in source.Attributes)
{
eventFormatting.AppendLine(sourceAttribute.FormatCSharpAttributeSignatureSyntax());
}
}
if (includeSecurity)
{
eventFormatting.Append($"{source.Security.FormatCSharpSyntax()} ");
}
if (includeKeywords)
{
if (source.IsStatic) eventFormatting.Append($"{Keywords.Static} ");
if (source.IsSealed) eventFormatting.Append($"{Keywords.Sealed} ");
if (includeAbstractKeyword & source.IsAbstract) eventFormatting.Append($"{Keywords.Abstract} ");
if (source.IsOverride) eventFormatting.Append($"{Keywords.Override} ");
if (source.IsVirtual) eventFormatting.Append($"{Keywords.Virtual} ");
}
eventFormatting.Append($"{source.EventType.FormatCSharpFullTypeName()} {source.Name}");
return eventFormatting.ToString();
}
/// <summary>
/// Gets the hash code for a formatted event signature using the C# format.
/// </summary>
/// <param name="source">The sources <see cref="IDotNetEvent"/> model.</param>
/// <param name="includeSecurity">Optional parameter that determines to generate security in the definition. By default this is false.</param>
/// <param name="includeAttributes">Optional parameter that determines if the attributes should be included in the definition. By default this is false.</param>
/// <param name="includeKeywords">Optional parameter that determines if all keywords are included in the definition. By default this is false.</param>
/// <returns>The hash code of the formatted model.</returns>
/// <exception cref="ArgumentNullException">This is thrown if the model is null.</exception>
public static int FormatCSharpComparisonHashCode(this IDotNetEvent source, bool includeSecurity = false,
bool includeAttributes = false, bool includeKeywords = false)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return source.FormatCSharpDeclarationSyntax(includeSecurity, includeAttributes, includeKeywords).GetHashCode();
}
/// <summary>
/// Generates the syntax definition of an method in c# syntax.
/// </summary>
/// <param name="source">The source <see cref="IDotNetMethod"/> model to generate.</param>
/// <param name="includeSecurity">Includes the security scope which was defined in the model.</param>
/// <param name="includeAttributes">Includes definition of the attributes assigned to the model.</param>
/// <param name="includeKeywords">Includes all keywords assigned to the source model.</param>
/// <param name="includeAbstractKeyword">Will include the definition for the abstract keyword in the definition if it is defined. default is false.</param>
/// <returns>Fully formatted event definition or null if the event data could not be generated.</returns>
public static string FormatCSharpDeclarationSyntax(this IDotNetMethod source, bool includeSecurity = true,
bool includeAttributes = true, bool includeKeywords = true ,bool includeAbstractKeyword = false)
{
if (source == null) return null;
StringBuilder methodFormatting = new StringBuilder();
if (includeAttributes & source.HasAttributes)
{
foreach (var sourceAttribute in source.Attributes)
{
methodFormatting.AppendLine(sourceAttribute.FormatCSharpAttributeSignatureSyntax());
}
}
if (includeSecurity)
{
methodFormatting.Append($"{source.Security.FormatCSharpSyntax()} ");
}
if (includeKeywords)
{
if (source.IsStatic) methodFormatting.Append($"{Keywords.Static} ");
if (source.IsSealed) methodFormatting.Append($"{Keywords.Sealed} ");
if (includeAbstractKeyword & source.IsAbstract) methodFormatting.Append($"{Keywords.Abstract} ");
if (source.IsOverride) methodFormatting.Append($"{Keywords.Override} ");
if (source.IsVirtual) methodFormatting.Append($"{Keywords.Virtual} ");
}
methodFormatting.Append(source.IsVoid ? $"{Keywords.Void} {source.Name}" : $"{source.ReturnType.FormatCSharpFullTypeName()} {source.Name}");
if (source.IsGeneric)
methodFormatting.Append($"{source.GenericParameters.FormatCSharpGenericSignatureSyntax()}");
methodFormatting.Append(source.HasParameters
? source.Parameters.FormatCSharpParametersSignatureSyntax()
: $"{Symbols.ParametersDefinitionStart}{Symbols.ParametersDefinitionEnd}");
if (source.IsGeneric)
{
foreach (var sourceGenericParameter in source.GenericParameters)
{
var whereClause = sourceGenericParameter.FormatCSharpGenericWhereClauseSyntax();
if (!string.IsNullOrEmpty(whereClause)) methodFormatting.Append($" {whereClause}");
}
}
return methodFormatting.ToString();
}
/// <summary>
/// Gets the hash code for a formatted method signature using the C# format.
/// </summary>
/// <param name="source">The sources <see cref="IDotNetMethod"/> model.</param>
/// <param name="includeSecurity">Optional parameter that determines to generate security in the definition. By default this is false.</param>
/// <param name="includeAttributes">Optional parameter that determines if the attributes should be included in the definition. By default this is false.</param>
/// <param name="includeKeywords">Optional parameter that determines if all keywords are included in the definition. By default this is false.</param>
/// <returns>The hash code of the formatted model.</returns>
/// <exception cref="ArgumentNullException">This is thrown if the model is null.</exception>
public static int FormatCSharpComparisonHashCode(this IDotNetMethod source, bool includeSecurity = false,
bool includeAttributes = false, bool includeKeywords = false)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return source.FormatCSharpDeclarationSyntax(includeSecurity, includeAttributes, includeKeywords).GetHashCode();
}
}
}