Skip to content

Commit 81a7628

Browse files
Merge pull request #3 from jasonwoods-7/main
Allow attributes on enum members
2 parents 3189f4f + b7b73dc commit 81a7628

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

Diff for: README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ Benchmark done with [BenchmarkDotNet](https://benchmarkdotnet.org/). You can run
1818
## Quick Start
1919

2020
1. Add source generator reference to your project
21-
```
21+
```xml
2222
<ItemGroup>
2323
<ProjectReference Include="path\to\EnumFastToStringDotNet.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
2424
</ItemGroup>
2525
```
2626
2. Add using statement
27-
```
27+
```c#
2828
using EnumFastToStringGenerated;
2929
```
3030
3. Add [FastToString] attribute to your enum
31-
```
31+
```c#
3232
[FastToString]
3333
public enum HumanStates
3434
{
@@ -40,12 +40,12 @@ public enum HumanStates
4040
}
4141
```
4242
4. Call FastToString method
43-
```
43+
```c#
4444
Console.Out.WriteLine(HumanStates.Dead.FastToString());
4545
```
4646

4747
5. The Automatically Generated Code
48-
```
48+
```c#
4949
using System;
5050
namespace EnumFastToStringGenerated
5151
{
@@ -70,7 +70,7 @@ namespace EnumFastToStringGenerated
7070
## Custom Method Name
7171

7272
If you would like to use a different method name, you can do so with the attribute argument "MethodName".
73-
```
73+
```c#
7474
[FastToString(MethodName = "CustomMethodName")]
7575
public enum HumanStates
7676
{
@@ -82,14 +82,14 @@ public enum HumanStates
8282
}
8383
```
8484
Then you can call the method like this:
85-
```
85+
```c#
8686
Console.Out.WriteLine(HumanStates.Dead.CustomMethodName());
8787
```
8888

8989
## Customizing Source Generator
9090

9191
You can customize the source generator by changing the const values in the EnumSwitchSourceGenerator.cs file.
92-
```
92+
```c#
9393
//The namespace that the attribute and extension method classes are defined in
9494
private const string NAMESPACE = "EnumFastToStringGenerated";
9595

Diff for: samples/Benchmark/Program.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.ComponentModel;
23
using BenchmarkDotNet.Attributes;
34
using BenchmarkDotNet.Running;
45
using EnumFastToStringGenerated;
@@ -78,6 +79,7 @@ public string SwitchExpressionToString(HumanStates state)
7879
[FastToString]
7980
public enum HumanStates
8081
{
82+
[Description("Not doing anything")]
8183
Idle,
8284
Working,
8385
Sleeping,

Diff for: src/EnumFastToStringDotNet/EnumSwitchSourceGenerator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static class EnumExtensions
5858
return states switch
5959
{{
6060
");
61-
foreach (string member in e.Members.Select(x => x.ToString()))
61+
foreach (string member in e.Members.Select(x => x.Identifier.ValueText))
6262
sourceBuilder.AppendLine($@" {symbolName}.{member} => nameof({symbolName}.{member}),");
6363
sourceBuilder.Append(@" _ => throw new ArgumentOutOfRangeException(nameof(states), states, null)
6464
};

0 commit comments

Comments
 (0)