@@ -113,7 +113,7 @@ public sealed class {SourceGeneratorHelper.AttributeName}Attribute : Attribute
113
113
namespace { SourceGeneratorHelper . NameSpace }
114
114
{{
115
115
/// <summary>
116
- /// Provides extension methods for operations related to the { symbol . Name } enumeration.
116
+ /// Provides extension methods for operations related to the <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration.
117
117
/// </summary>
118
118
{ symbol . DeclaredAccessibility . ToString ( "G" ) . ToLower ( ) } static class { symbol . Name } EnumExtensions
119
119
{{" ) ;
@@ -147,6 +147,10 @@ namespace {SourceGeneratorHelper.NameSpace}
147
147
148
148
//GetLength
149
149
GetLengthFast ( sourceBuilder , symbol , e ) ;
150
+
151
+
152
+ //TryParse
153
+ TryParseFast ( sourceBuilder , e , symbol ) ;
150
154
151
155
sourceBuilder . Append ( @"
152
156
}
@@ -163,11 +167,11 @@ private static void ToDisplay(StringBuilder sourceBuilder, ISymbol symbol, EnumD
163
167
{
164
168
sourceBuilder . Append ( $@ "
165
169
/// <summary>
166
- /// Converts the <see cref=""{ symbol . Name } "" /> enumeration value to its display string.
170
+ /// Converts the <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration value to its display string.
167
171
/// </summary>
168
- /// <param name=""states"">The <see cref=""{ symbol . Name } "" /> enumeration value.</param>
172
+ /// <param name=""states"">The <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration value.</param>
169
173
/// <param name=""defaultValue"">The default value to return if the enumeration value is not recognized.</param>
170
- /// <returns>The display string of the <see cref=""{ symbol . Name } "" /> value.</returns>
174
+ /// <returns>The display string of the <see cref=""global:: { symbol . FullName ( ) } "" /> value.</returns>
171
175
public static string { SourceGeneratorHelper . ExtensionMethodNameToDisplay } (this { symbol . FullName ( ) } states, string defaultValue = null)
172
176
{{
173
177
return states switch
@@ -195,11 +199,11 @@ private static void ToDescription(StringBuilder sourceBuilder, ISymbol symbol, E
195
199
{
196
200
sourceBuilder . Append ( $@ "
197
201
/// <summary>
198
- /// Gets the description of the <see cref=""{ symbol . Name } "" /> enumeration value.
202
+ /// Gets the description of the <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration value.
199
203
/// </summary>
200
- /// <param name=""states"">The <see cref=""{ symbol . Name } "" /> enumeration value.</param>
204
+ /// <param name=""states"">The <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration value.</param>
201
205
/// <param name=""defaultValue"">The default value to return if the enumeration value is not recognized.</param>
202
- /// <returns>The description of the <see cref=""{ symbol . Name } "" /> value.</returns>
206
+ /// <returns>The description of the <see cref=""global:: { symbol . FullName ( ) } "" /> value.</returns>
203
207
public static string { SourceGeneratorHelper . ExtensionMethodNameToDescription } (this { symbol . FullName ( ) } states, string defaultValue = null)
204
208
{{
205
209
return states switch
@@ -226,10 +230,10 @@ private static void IsDefinedString(StringBuilder sourceBuilder, EnumDeclaration
226
230
{
227
231
sourceBuilder . Append ( $@ "
228
232
/// <summary>
229
- /// Checks if the specified string represents a defined <see cref=""{ symbol . Name } "" /> value.
233
+ /// Checks if the specified string represents a defined <see cref=""global:: { symbol . FullName ( ) } "" /> value.
230
234
/// </summary>
231
- /// <param name=""states"">The string representing a <see cref=""{ symbol . Name } "" /> value.</param>
232
- /// <returns>True if the string represents a defined <see cref=""{ symbol . Name } "" /> value; otherwise, false.</returns>
235
+ /// <param name=""states"">The string representing a <see cref=""global:: { symbol . FullName ( ) } "" /> value.</param>
236
+ /// <returns>True if the string represents a defined <see cref=""global:: { symbol . FullName ( ) } "" /> value; otherwise, false.</returns>
233
237
public static bool { SourceGeneratorHelper . ExtensionMethodNameIsDefined } (string states)
234
238
{{
235
239
return states switch
@@ -248,10 +252,10 @@ private static void IsDefinedEnum(StringBuilder sourceBuilder, ISymbol symbol, E
248
252
{
249
253
sourceBuilder . Append ( $@ "
250
254
/// <summary>
251
- /// Checks if the specified <see cref=""{ symbol . Name } "" /> value is defined.
255
+ /// Checks if the specified <see cref=""global:: { symbol . FullName ( ) } "" /> value is defined.
252
256
/// </summary>
253
- /// <param name=""states"">The <see cref=""{ symbol . Name } "" /> value to check.</param>
254
- /// <returns>True if the <see cref=""{ symbol . Name } "" /> value is defined; otherwise, false.</returns>
257
+ /// <param name=""states"">The <see cref=""global:: { symbol . FullName ( ) } "" /> value to check.</param>
258
+ /// <returns>True if the <see cref=""global:: { symbol . FullName ( ) } "" /> value is defined; otherwise, false.</returns>
255
259
public static bool { SourceGeneratorHelper . ExtensionMethodNameIsDefined } ({ symbol . FullName ( ) } states)
256
260
{{
257
261
return states switch
@@ -270,11 +274,11 @@ private static void ToStringFast(StringBuilder sourceBuilder, ISymbol symbol, En
270
274
{
271
275
sourceBuilder . Append ( $@ "
272
276
/// <summary>
273
- /// Converts the <see cref=""{ symbol . Name } "" /> enumeration value to its string representation.
277
+ /// Converts the <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration value to its string representation.
274
278
/// </summary>
275
- /// <param name=""states"">The <see cref=""{ symbol . Name } "" /> enumeration value.</param>
279
+ /// <param name=""states"">The <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration value.</param>
276
280
/// <param name=""defaultValue"">The default value to return if the enumeration value is not recognized.</param>
277
- /// <returns>The string representation of the <see cref=""{ symbol . Name } "" /> value.</returns>
281
+ /// <returns>The string representation of the <see cref=""global:: { symbol . FullName ( ) } "" /> value.</returns>
278
282
public static string { SourceGeneratorHelper . ExtensionMethodNameToString } (this { symbol . FullName ( ) } states, string defaultValue = null)
279
283
{{
280
284
return states switch
@@ -294,7 +298,7 @@ private static void DisplayNamesDictionary(StringBuilder sourceBuilder, ISymbol
294
298
{
295
299
sourceBuilder . Append ( $@ "
296
300
/// <summary>
297
- /// Provides a dictionary that maps <see cref=""{ symbol . Name } "" /> values to their corresponding display names.
301
+ /// Provides a dictionary that maps <see cref=""global:: { symbol . FullName ( ) } "" /> values to their corresponding display names.
298
302
/// </summary>
299
303
public static readonly ImmutableDictionary<{ symbol . FullName ( ) } , string> { SourceGeneratorHelper . PropertyDisplayNamesDictionary } = new Dictionary<{ symbol . FullName ( ) } , string>
300
304
{{
@@ -319,7 +323,7 @@ private static void DisplayDescriptionsDictionary(StringBuilder sourceBuilder, I
319
323
{
320
324
sourceBuilder . Append ( $@ "
321
325
/// <summary>
322
- /// Provides a dictionary that maps <see cref=""{ symbol . Name } "" /> values to their corresponding descriptions.
326
+ /// Provides a dictionary that maps <see cref=""global:: { symbol . FullName ( ) } "" /> values to their corresponding descriptions.
323
327
/// </summary>
324
328
public static readonly ImmutableDictionary<{ symbol . FullName ( ) } , string> { SourceGeneratorHelper . PropertyDisplayDescriptionsDictionary } = new Dictionary<{ symbol . FullName ( ) } , string>
325
329
{{
@@ -343,9 +347,9 @@ private static void GetValuesFast(StringBuilder sourceBuilder, ISymbol symbol, E
343
347
{
344
348
sourceBuilder . Append ( $@ "
345
349
/// <summary>
346
- /// Retrieves an array of all <see cref=""{ symbol . Name } "" /> enumeration values.
350
+ /// Retrieves an array of all <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration values.
347
351
/// </summary>
348
- /// <returns>An array containing all <see cref=""{ symbol . Name } "" /> enumeration values.</returns>
352
+ /// <returns>An array containing all <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration values.</returns>
349
353
public static { symbol . FullName ( ) } [] { SourceGeneratorHelper . ExtensionMethodNameGetValues } ()
350
354
{{
351
355
return new[]
@@ -363,9 +367,9 @@ private static void GetNamesFast(StringBuilder sourceBuilder, ISymbol symbol, En
363
367
{
364
368
sourceBuilder . Append ( $@ "
365
369
/// <summary>
366
- /// Retrieves an array of strings containing the names of all <see cref=""{ symbol . Name } "" /> enumeration values.
370
+ /// Retrieves an array of strings containing the names of all <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration values.
367
371
/// </summary>
368
- /// <returns>An array of strings containing the names of all <see cref=""{ symbol . Name } "" /> enumeration values.</returns>
372
+ /// <returns>An array of strings containing the names of all <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration values.</returns>
369
373
public static string[] { SourceGeneratorHelper . ExtensionMethodNameGetNames } ()
370
374
{{
371
375
return new[]
@@ -383,9 +387,9 @@ private static void GetLengthFast(StringBuilder sourceBuilder, ISymbol symbol, E
383
387
{
384
388
sourceBuilder . Append ( $@ "
385
389
/// <summary>
386
- /// Gets the length of the <see cref=""{ symbol . Name } "" /> enumeration.
390
+ /// Gets the length of the <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration.
387
391
/// </summary>
388
- /// <returns>The length of the <see cref=""{ symbol . Name } "" /> enumeration.</returns>
392
+ /// <returns>The length of the <see cref=""global:: { symbol . FullName ( ) } "" /> enumeration.</returns>
389
393
public static int { SourceGeneratorHelper . ExtensionMethodNameGetLength } ()
390
394
{{
391
395
return { e . Members . Count } ;
@@ -395,4 +399,39 @@ private static void GetLengthFast(StringBuilder sourceBuilder, ISymbol symbol, E
395
399
}
396
400
" ) ;
397
401
}
402
+
403
+ private static void TryParseFast ( StringBuilder sourceBuilder , EnumDeclarationSyntax e , ISymbol symbol )
404
+ {
405
+ sourceBuilder . Append ( $@ "
406
+ /// <summary>
407
+ /// Try parse a string to <see cref=""global::{ symbol . FullName ( ) } "" /> value.
408
+ /// </summary>
409
+ /// <param name=""states"">The string representing a <see cref=""global::{ symbol . FullName ( ) } "" /> value.</param>
410
+ /// <param name=""result"">The enum <see cref=""global::{ symbol . FullName ( ) } "" /> parse result.</param>
411
+ /// <returns>True if the string is parsed successfully; otherwise, false.</returns>
412
+ public static bool { SourceGeneratorHelper . ExtensionMethodNameTryParse } (string states, out { symbol . FullName ( ) } result)
413
+ {{
414
+ switch (states)
415
+ {{
416
+ " ) ;
417
+ foreach ( var member in e . Members . Select ( x => x . Identifier . ValueText ) )
418
+ sourceBuilder . AppendLine (
419
+ $$ """
420
+ case "{{ member }} ":
421
+ {
422
+ result = {{ symbol . FullName ( ) }} .{{ member }} ;
423
+ return true;
424
+ }
425
+ """ ) ;
426
+ sourceBuilder . Append (
427
+ """
428
+ default: {
429
+ result = default;
430
+ return false;
431
+ }
432
+ }
433
+ }
434
+ """
435
+ ) ;
436
+ }
398
437
}
0 commit comments