RSCG – FusionReactor

name FusionReactor
nuget https://www.nuget.org/packages/FusionReactor.SourceGenerators.EnumExtensions
link https://github.com/OhFlowi/FusionReactor.SourceGenerators.EnumExtensions
author OhFlowi

Enums to string and other extensions

 

This is how you can use FusionReactor .

The code that you start with is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
<Project Sdk="Microsoft.NET.Sdk">
 
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
 
  <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>
 
  <ItemGroup>
    <PackageReference Include="FusionReactor.SourceGenerators.EnumExtensions" Version="1.1.0" />
  </ItemGroup>
 
   
</Project>

The code that you will use is

1
2
3
using EnumClassDemo;
Console.WriteLine(Colors.None.GetName());
Console.WriteLine(ColorsExtensions.GetContent()[Colors.None]);
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
 
namespace EnumClassDemo;
 
//[Flags]
[FusionReactor.SourceGenerators.EnumExtensions.GenerateEnumExtensions]
public enum Colors
{
    [Display(
         ShortName = "None",
         Name = "none - 0",
         Description = "Zero",
         Prompt = "ooF",
         GroupName = "Color1",
         Order = 0)]
    None =0,
    Red=1,
    Green=2,
    Blue=4,
}

 

The code that is generated is

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
// <auto-generated />
#nullable enable
using System;
using System.CodeDom.Compiler;
using System.Collections;
#if NET8_0_OR_GREATER
using System.Collections.Frozen;
#endif
using System.Collections.Generic;
using System.Collections.ObjectModel;
 
namespace EnumClassDemo;
/// <summary>
/// Extension methods for the <see cref = "Colors"/> enum.
/// </summary>
[GeneratedCode("FusionReactor.SourceGenerators.EnumExtensions", null)]
public static partial class ColorsExtensions
{
#if NET8_0_OR_GREATER
private static readonly FrozenDictionary<Colors, Int32> content
  = new Dictionary<Colors, Int32>
    {
        { Colors.None, 0 },
{ Colors.Red, 1 },
{ Colors.Green, 2 },
{ Colors.Blue, 4 },
 
    }
    .ToFrozenDictionary();
#else
    private static readonly Dictionary<Colors, Int32> contentDictionary = new Dictionary<Colors, Int32>
    {
        {
            Colors.None,
            0
        },
        {
            Colors.Red,
            1
        },
        {
            Colors.Green,
            2
        },
        {
            Colors.Blue,
            4
        },
    };
    private static readonly IReadOnlyDictionary<Colors, Int32> content = new ReadOnlyDictionary<Colors, Int32>(contentDictionary);
#endif
#if NET8_0_OR_GREATER
private static readonly FrozenSet<string> names = new []
{
    "None",
"Red",
"Green",
"Blue",
 
}
.ToFrozenSet();
#elif NET5_0_OR_GREATER
private static readonly IReadOnlySet<string> names = new HashSet<string>()
{
    "None",
"Red",
"Green",
"Blue",
 
};
#else
    private static readonly HashSet<string> names = new HashSet<string>()
    {
        "None",
        "Red",
        "Green",
        "Blue",
    };
#endif
#if NET8_0_OR_GREATER
private static readonly FrozenSet<Colors> values = new []
{
    Colors.None,
Colors.Red,
Colors.Green,
Colors.Blue,
 
}
.ToFrozenSet();
#elif NET5_0_OR_GREATER
private static readonly IReadOnlySet<Colors> values = new HashSet<Colors>()
{
    Colors.None,
Colors.Red,
Colors.Green,
Colors.Blue,
 
};
#else
    private static readonly HashSet<Colors> values = new HashSet<Colors>()
    {
        Colors.None,
        Colors.Red,
        Colors.Green,
        Colors.Blue,
    };
#endif
    /// <summary>
    /// Gets the content dictionary containing mappings of <see cref = "Colors"/> enum values to values.
    /// </summary>
    /// <returns>The read-only content dictionary.</returns>
     
#if NET8_0_OR_GREATER
public static FrozenDictionary<Colors, Int32> GetContent()
#else
    public static IReadOnlyDictionary<Colors, Int32> GetContent()
#endif
    {
        return content;
    }
 
    /// <summary>
    /// Gets the content dictionary containing mappings of <see cref = "Colors"/> enum values to values.
    /// </summary>
    /// <param name = "enumValue">The enum value for which to get the content dictionary.</param>
    /// <returns>The read-only content dictionary.</returns>
     
#if NET8_0_OR_GREATER
public static FrozenDictionary<Colors, Int32> GetContent(this Colors enumValue)
#else
    public static IReadOnlyDictionary<Colors, Int32> GetContent(this Colors enumValue)
#endif
    {
        return content;
    }
 
    /// <summary>
    /// Retrieves the name of the constant in the <see cref = "Colors"/>.
    /// </summary>
    /// <param name = "enumValue">The enum value to convert.</param>
    /// <returns>
    /// A string containing the name of the <see cref = "Colors"/>;
    /// or <see langword="null"/> if no such constant is found.
    /// </returns>
    public static string? GetName(this Colors enumValue)
    {
        return enumValue switch
        {
            Colors.None => nameof(Colors.None),
            Colors.Red => nameof(Colors.Red),
            Colors.Green => nameof(Colors.Green),
            Colors.Blue => nameof(Colors.Blue),
            _ => null
        };
    }
 
    /// <summary>
    /// Retrieves all available names of the <see cref = "Colors"/>.
    /// </summary>
    /// <returns>An enumerable collection of <see cref = "Colors"/> names.</returns>
     
#if NET8_0_OR_GREATER
public static FrozenSet<string> GetNames()
#elif NET5_0_OR_GREATER
public static IReadOnlySet<string> GetNames()
#else
    public static HashSet<string> GetNames()
#endif
    {
        return names;
    }
 
    /// <summary>
    /// Retrieves all available names of the <see cref = "Colors"/>.
    /// </summary>
    /// <param name = "enumValue">The enumeration value.</param>
    /// <returns>An enumerable collection of <see cref = "Colors"/> names.</returns>
     
#if NET8_0_OR_GREATER
public static FrozenSet<string> GetNames(this Colors enumValue)
#elif NET5_0_OR_GREATER
public static IReadOnlySet<string> GetNames(this Colors enumValue)
#else
    public static HashSet<string> GetNames(this Colors enumValue)
#endif
    {
        return names;
    }
 
    /// <summary>
    /// Retrieves all available values of the <see cref = "Colors"/>.
    /// </summary>
    /// <returns>An enumerable collection of <see cref = "Colors"/> values.</returns>
     
#if NET8_0_OR_GREATER
public static FrozenSet<Colors> GetValues()
#elif NET5_0_OR_GREATER
public static IReadOnlySet<Colors> GetValues()
#else
    public static HashSet<Colors> GetValues()
#endif
    {
        return values;
    }
 
    /// <summary>
    /// Retrieves all available values of the <see cref = "Colors"/>.
    /// </summary>
    /// <param name = "enumValue">The enumeration value.</param>
    /// <returns>An enumerable collection of <see cref = "Colors"/> values.</returns>
     
#if NET8_0_OR_GREATER
public static FrozenSet<Colors> GetValues(this Colors enumValue)
#elif NET5_0_OR_GREATER
public static IReadOnlySet<Colors> GetValues(this Colors enumValue)
#else
    public static HashSet<Colors> GetValues(this Colors enumValue)
#endif
    {
        return values;
    }
 
    /// <summary>
    /// Parses the specified string representation of the enumeration value to its corresponding
    /// <see cref = "Colors"/> value.
    /// </summary>
    /// <param name = "value">A string containing the name or value to convert.</param>
    /// <param name = "ignoreCase">
    /// A boolean indicating whether to ignore case during the parsing. Default is <c>false</c>.
    /// </param>
    /// <returns>
    /// The <see cref = "Colors"/> value equivalent to the specified string representation.
    /// </returns>
    public static Colors Parse(string value, bool ignoreCase = false)
    {
        if (ignoreCase)
        {
            return value.ToLowerInvariant() switch
            {
                "none" => Colors.None,
                "red" => Colors.Red,
                "green" => Colors.Green,
                "blue" => Colors.Blue,
                _ => throw new ArgumentException(),
            };
        }
        else
        {
            return value switch
            {
                "None" => Colors.None,
                "Red" => Colors.Red,
                "Green" => Colors.Green,
                "Blue" => Colors.Blue,
                _ => throw new ArgumentException(),
            };
        }
    }
 
    /// <summary>
    /// Parses the specified string representation of the enumeration value to its corresponding
    /// <see cref = "Colors"/> value.
    /// </summary>
    /// <param name = "enumValue">The current <see cref = "Colors"/> value.</param>
    /// <param name = "value">A string containing the name or value to convert.</param>
    /// <param name = "ignoreCase">
    /// A boolean indicating whether to ignore case during the parsing. Default is <c>false</c>.
    /// </param>
    /// <returns>
    /// The <see cref = "Colors"/> value equivalent to the specified string representation.
    /// </returns>
    public static Colors Parse(this Colors enumValue, string value, bool ignoreCase = false)
    {
        if (ignoreCase)
        {
            return value.ToLowerInvariant() switch
            {
                "none" => Colors.None,
                "red" => Colors.Red,
                "green" => Colors.Green,
                "blue" => Colors.Blue,
                _ => throw new ArgumentException(),
            };
        }
        else
        {
            return value switch
            {
                "None" => Colors.None,
                "Red" => Colors.Red,
                "Green" => Colors.Green,
                "Blue" => Colors.Blue,
                _ => throw new ArgumentException(),
            };
        }
    }
 
    /// <summary>
    /// Tries to parse the specified string representation of an enumeration value to its corresponding
    /// <see cref = "Colors"/> enumeration value.
    /// </summary>
    /// <param name = "value">The string representation of the enumeration value.</param>
    /// <param name = "result">
    /// When this method returns, contains the <see cref = "Colors"/> value equivalent
    /// to the string representation, if the parse succeeded, or default(Colors) if the parse failed.</param>
    /// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
    public static bool TryParse(string value, out Colors? result)
    {
        return TryParse(value, false, out result);
    }
 
    /// <summary>
    /// Tries to parse the specified string representation of an enumeration value to its corresponding
    /// <see cref = "Colors"/> enumeration value.
    /// </summary>
    /// <param name = "value">The string representation of the enumeration value.</param>
    /// <param name = "ignoreCase">A boolean indicating whether case should be ignored when parsing.</param>
    /// <param name = "result">
    /// When this method returns, contains the <see cref = "Colors"/> value equivalent
    /// to the string representation, if the parse succeeded, or default(Colors) if the parse failed.</param>
    /// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
    public static bool TryParse(string value, bool ignoreCase, out Colors? result)
    {
        if (ignoreCase)
        {
            result = value.ToLowerInvariant() switch
            {
                "none" => Colors.None,
                "red" => Colors.Red,
                "green" => Colors.Green,
                "blue" => Colors.Blue,
                _ => null,
            };
        }
        else
        {
            result = value switch
            {
                "None" => Colors.None,
                "Red" => Colors.Red,
                "Green" => Colors.Green,
                "Blue" => Colors.Blue,
                _ => null,
            };
        }
 
        return result != null;
    }
 
    /// <summary>
    /// Tries to parse the specified string representation of an enumeration value to its corresponding
    /// <see cref = "Colors"/> enumeration value.
    /// </summary>
    /// <param name = "enumValue">The enumeration value to parse.</param>
    /// <param name = "value">The string representation of the enumeration value.</param>
    /// <param name = "result">
    /// When this method returns, contains the <see cref = "Colors"/> value equivalent
    /// to the string representation, if the parse succeeded, or default(Colors) if the parse failed.</param>
    /// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
    public static bool TryParse(this Colors enumValue, string value, out Colors? result)
    {
        return TryParse(value, false, out result);
    }
 
    /// <summary>
    /// Tries to parse the specified string representation of an enumeration value to its corresponding
    /// <see cref = "Colors"/> enumeration value.
    /// </summary>
    /// <param name = "enumValue">The enumeration value to parse.</param>
    /// <param name = "value">The string representation of the enumeration value.</param>
    /// <param name = "ignoreCase">A boolean indicating whether case should be ignored when parsing.</param>
    /// <param name = "result">
    /// When this method returns, contains the <see cref = "Colors"/> value equivalent
    /// to the string representation, if the parse succeeded, or default(Colors) if the parse failed.</param>
    /// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
    public static bool TryParse(this Colors enumValue, string value, bool ignoreCase, out Colors? result)
    {
        if (ignoreCase)
        {
            result = value.ToLowerInvariant() switch
            {
                "none" => Colors.None,
                "red" => Colors.Red,
                "green" => Colors.Green,
                "blue" => Colors.Blue,
                _ => null,
            };
        }
        else
        {
            result = value switch
            {
                "None" => Colors.None,
                "Red" => Colors.Red,
                "Green" => Colors.Green,
                "Blue" => Colors.Blue,
                _ => null,
            };
        }
 
        return result != null;
    }
}
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
// <auto-generated />
#nullable enable
using System;
using System.Collections;
#if NET8_0_OR_GREATER
using System.Collections.Frozen;
#endif
using System.Collections.Generic;
using System.Collections.ObjectModel;
using FusionReactor.SourceGenerators.EnumExtensions;
 
namespace EnumClassDemo;
public static partial class ColorsExtensions
{
#if !NET8_0_OR_GREATER
    private static readonly Dictionary<Colors, DisplayResult?> displayResultsDictionary = new Dictionary<Colors, DisplayResult?>
    {
        {
            Colors.None,
            new DisplayResult
            {
                ShortName = "None",
                Name = "none - 0",
                Description = "Zero",
                Prompt = "ooF",
                GroupName = "Color1",
                Order = 0,
            }
        },
        {
            Colors.Red,
            null
        },
        {
            Colors.Green,
            null
        },
        {
            Colors.Blue,
            null
        },
    };
#endif
    /// <summary>
    /// Returns the <see cref = "System.ComponentModel.DataAnnotations.DisplayAttribute"/> of the <see cref = "Colors"/> enum.
    /// </summary>
    /// <returns>The display attribute result or the enum value.</returns>
     
#if NET8_0_OR_GREATER
public static FrozenDictionary<Colors, DisplayResult?> DisplayResults
  => new Dictionary<Colors, DisplayResult?>
    {
        { Colors.None, new DisplayResult {ShortName = "None",
Name = "none - 0",
Description = "Zero",
Prompt = "ooF",
GroupName = "Color1",
Order = 0,
}},
{ Colors.Red, null },
{ Colors.Green, null },
{ Colors.Blue, null },
 
    }
    .ToFrozenDictionary();
#else
    public static IReadOnlyDictionary<Colors, DisplayResult?> DisplayResults => new ReadOnlyDictionary<Colors, DisplayResult?>(displayResultsDictionary);
 
#endif
    /// <summary>
    /// Returns the <see cref = "System.ComponentModel.DataAnnotations.DisplayAttribute.ShortName"/> of the <see cref = "Colors"/> enum.
    /// </summary>
    /// <param name = "enumValue">The enum value.</param>
    /// <returns>The display name or the enum value.</returns>
    public static string? DisplayShortName(this Colors enumValue)
    {
        return enumValue switch
        {
            Colors.None => "None",
            Colors.Red => null,
            Colors.Green => null,
            Colors.Blue => null,
            _ => null
        };
    }
 
    /// <summary>
    /// Returns the <see cref = "System.ComponentModel.DataAnnotations.DisplayAttribute.Name"/> of the <see cref = "Colors"/> enum.
    /// </summary>
    /// <param name = "enumValue">The enum value.</param>
    /// <returns>The name or the enum value.</returns>
    public static string? DisplayName(this Colors enumValue)
    {
        return enumValue switch
        {
            Colors.None => "none - 0",
            Colors.Red => null,
            Colors.Green => null,
            Colors.Blue => null,
            _ => null
        };
    }
 
    /// <summary>
    /// Returns the <see cref = "System.ComponentModel.DataAnnotations.DisplayAttribute.Description"/> of the <see cref = "Colors"/> enum.
    /// </summary>
    /// <param name = "enumValue">The enum value.</param>
    /// <returns>The display name or the enum value.</returns>
    public static string? DisplayDescription(this Colors enumValue)
    {
        return enumValue switch
        {
            Colors.None => "Zero",
            Colors.Red => null,
            Colors.Green => null,
            Colors.Blue => null,
            _ => null
        };
    }
 
    /// <summary>
    /// Returns the <see cref = "System.ComponentModel.DataAnnotations.DisplayAttribute.Prompt"/> of the <see cref = "Colors"/> enum.
    /// </summary>
    /// <param name = "enumValue">The enum value.</param>
    /// <returns>The display name or the enum value.</returns>
    public static string? DisplayPrompt(this Colors enumValue)
    {
        return enumValue switch
        {
            Colors.None => "ooF",
            Colors.Red => null,
            Colors.Green => null,
            Colors.Blue => null,
            _ => null
        };
    }
 
    /// <summary>
    /// Returns the <see cref = "System.ComponentModel.DataAnnotations.DisplayAttribute.GroupName"/> of the <see cref = "Colors"/> enum.
    /// </summary>
    /// <param name = "enumValue">The enum value.</param>
    /// <returns>The display name or the enum value.</returns>
    public static string? DisplayGroupName(this Colors enumValue)
    {
        return enumValue switch
        {
            Colors.None => "Color1",
            Colors.Red => null,
            Colors.Green => null,
            Colors.Blue => null,
            _ => null
        };
    }
 
    /// <summary>
    /// Returns the <see cref = "System.ComponentModel.DataAnnotations.DisplayAttribute.Order"/> of the <see cref = "Colors"/> enum.
    /// </summary>
    /// <param name = "enumValue">The enum value.</param>
    /// <returns>The display name or the enum value.</returns>
    public static int? DisplayOrder(this Colors enumValue)
    {
        return enumValue switch
        {
            Colors.None => 0,
            Colors.Red => null,
            Colors.Green => null,
            Colors.Blue => null,
            _ => null
        };
    }
}
01
02
03
04
05
06
07
08
09
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
// <auto-generated />
#nullable enable
using System;
using System.CodeDom.Compiler;
 
namespace FusionReactor.SourceGenerators.EnumExtensions;
/// <inheritdoc cref = "System.ComponentModel.DataAnnotations.DisplayAttribute"/>
[GeneratedCode("FusionReactor.SourceGenerators.EnumExtensions", null)]
public class DisplayResult
{
    /// <summary>
    /// Gets or sets the ShortName attribute property, which may be a resource key string.
    /// </summary>
    public string? ShortName { get; set; }
    /// <summary>
    /// Gets or sets the Name attribute property, which may be a resource key string.
    /// </summary>
    public string? Name { get; set; }
    /// <summary>
    /// Gets or sets the Description attribute property, which may be a resource key string.
    /// </summary>
    public string? Description { get; set; }
    /// <summary>
    /// Gets or sets the Prompt attribute property, which may be a resource key string.
    /// </summary>
    public string? Prompt { get; set; }
    /// <summary>
    /// Gets or sets the GroupName attribute property, which may be a resource key string.
    /// </summary>
    public string? GroupName { get; set; }
    /// <summary>
    /// Gets or sets the order in which this field should be displayed.  If this property is not set then
    /// the presentation layer will automatically determine the order.  Setting this property explicitly
    /// allows an override of the default behavior of the presentation layer.
    /// </summary>
    public int? Order { get; set; }
}
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
// <auto-generated />
using System;
using System.CodeDom.Compiler;
 
namespace FusionReactor.SourceGenerators.EnumExtensions;
/// <summary>
/// Attribute to mark an enum for FusionReactor.SourceGenerators.EnumExtensions extension generations.
/// </summary>
/// <remarks>
/// This attribute is used to mark an enum for FusionReactor.SourceGenerators.EnumExtensions extension generations.
/// </remarks>
[GeneratedCode("FusionReactor.SourceGenerators.EnumExtensions", null)]
[AttributeUsage(AttributeTargets.Enum)]
public class GenerateEnumExtensionsAttribute : Attribute
{
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/FusionReactor