Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/andrewlock/NetEscapades.EnumGenerators

A source generator for generating fast "reflection" methods for enums
https://github.com/andrewlock/NetEscapades.EnumGenerators

Last synced: 3 months ago
JSON representation

A source generator for generating fast "reflection" methods for enums

Awesome Lists containing this project

README

        

# NetEscapades.EnumGenerators

![Build status](https://github.com/andrewlock/NetEscapades.EnumGenerators/actions/workflows/BuildAndPack.yml/badge.svg)
[![NuGet](https://img.shields.io/nuget/v/NetEscapades.EnumGenerators.svg)](https://www.nuget.org/packages/NetEscapades.EnumGenerators/)

A Source Generator package that generates extension methods for enums, to allow fast "reflection".

> This source generator requires the .NET 7 SDK. You can target earlier frameworks like .NET Core 3.1 etc, but the _SDK_ must be at least 7.0.100

Add the package to your application using

```bash
dotnet add package NetEscapades.EnumGenerators
```

This adds a `` to your project. You can additionally mark the package as `PrivateAssets="all"` and `ExcludeAssets="runtime"`.

> Setting `PrivateAssets="all"` means any projects referencing this one won't get a reference to the _NetEscapades.EnumGenerators_ package. Setting `ExcludeAssets="runtime"` ensures the _NetEscapades.EnumGenerators.Attributes.dll_ file is not copied to your build output (it is not required at runtime).

```xml


Exe
net8.0



```

Adding the package will automatically add a marker attribute, `[EnumExtensions]`, to your project.

To use the generator, add the `[EnumExtensions]` attribute to an enum. For example:

```csharp
[EnumExtensions]
public enum MyEnum
{
First,

[Display(Name = "2nd")]
Second,
}
```

This will generate a class called `MyEnumExtensions` (by default), which contains a number of helper methods. For example:

```csharp
public static partial class MyEnumExtensions
{
public const int Length = 2;

public static string ToStringFast(this MyEnum value)
=> value switch
{
MyEnum.First => nameof(MyEnum.First),
MyEnum.Second => "2nd",
_ => value.ToString(),
};

public static bool IsDefined(MyEnum value)
=> value switch
{
MyEnum.First => true,
MyEnum.Second => true,
_ => false,
};

public static bool IsDefined(string name)
=> name switch
{
nameof(MyEnum.First) => true,
nameof(MyEnum.Second) => true,
_ => false,
};

public static bool TryParse(
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)] string? name,
bool ignoreCase,
out MyEnum value)
=> ignoreCase ? TryParseIgnoreCase(name, out value) : TryParse(name, out value);

private static bool TryParseIgnoreCase(
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)] string? name,
out MyEnum value)
{
switch (name)
{
case { } s when s.Equals(nameof(MyEnum.First), System.StringComparison.OrdinalIgnoreCase):
value = MyEnum.First;
return true;
case { } s when s.Equals(nameof(MyEnum.Second), System.StringComparison.OrdinalIgnoreCase):
value = MyEnum.Second;
return true;
case { } s when int.TryParse(name, out var val):
value = (MyEnum)val;
return true;
default:
value = default;
return false;
}
}

public static bool TryParse(
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)] string? name,
out MyEnum value)
{
switch (name)
{
case nameof(MyEnum.First):
value = MyEnum.First;
return true;
case nameof(MyEnum.Second):
value = MyEnum.Second;
return true;
case { } s when int.TryParse(name, out var val):
value = (MyEnum)val;
return true;
default:
value = default;
return false;
}
}

public static MyEnum[] GetValues()
{
return new[]
{
MyEnum.First,
MyEnum.Second,
};
}

public static string[] GetNames()
{
return new[]
{
nameof(MyEnum.First),
nameof(MyEnum.Second),
};
}
}
```

If you create a "Flags" `enum` by decorating it with the `[Flags]` attribute, an additional method is created, which provides a bitwise alternative to the `Enum.HasFlag(flag)` method:

```csharp
public static bool HasFlagFast(this MyEnum value, MyEnum flag)
=> flag == 0 ? true : (value & flag) == flag;
```

Note that if you provide a `[Display]` or `[Description]` attribute, the value you provide for this attribute can be used by methods like `ToStringFast()` and `TryParse()` by passing the argument `allowMatchingMetadataAttribute: true`. Adding both attributes to an enum member is not supported, though conventionally the "first" attribute will be used.

You can override the name of the extension class by setting `ExtensionClassName` in the attribute and/or the namespace of the class by setting `ExtensionClassNamespace`. By default, the class will be public if the enum is public, otherwise it will be internal.

## Enabling interception

Interceptors were introduced as an experimental feature in C#12 with .NET 8. They allow a source generator to "intercept" certain method calls, and replace the call with a different one. _NetEscapades.EnumGenerators_ has support for intercepting `ToString()` and `HasFlag()` method calls.

> To use interceptors, you must be using at least version 8.0.400 of the .NET SDK. [This ships with Visual Studio version 17.11](https://learn.microsoft.com/en-us/dotnet/core/porting/versioning-sdk-msbuild-vs), so you will need at least that version or higher.

To enable interception for a project, update to the latest version of _NetEscapades.EnumGenerators_ and set the `EnableEnumGeneratorInterceptor` property in your _.csproj_ to `true`:

```xml


Exe
net8.0

true



```

This enables interception for all enums defined in the project that use the `[EnumExtensions]` or `[EnumExtensions]` attributes. If you wish to intercept calls made to enums with extensions defined in _other_ projects, you must add the `[Interceptable]` attribute in the project where you want the interception to happen, e.g.

```csharp
[assembly:Interceptable]
[assembly:Interceptable]
```

If you don't want a specific enum to be intercepted, you can set the `IsInterceptable` property to `false`, e.g.

```csharp
[EnumExtensions(IsInterceptable = false)]
public enum Colour
{
Red = 0,
Blue = 1,
}
```

Interception only works when the target type is unambiguously an interceptable enum, so it won't work

- When `ToString()` is called in other source generated code.
- When `ToString()` is called in already-compiled code.
- If the `ToString()` call is _implicit_ (for example in `string` interpolation)
- If the `ToString()` call is made on a base type, such as `System.Enum` or `object`
- If the `ToString()` call is made on a generic type

## Embedding the attributes in your project

By default, the `[EnumExtensions]` attributes referenced in your application are contained in an external dll. It is also possible to embed the attributes directly in your project, so they appear in the dll when your project is built. If you wish to do this, you must do two things:

1. Define the MSBuild constant `NETESCAPADES_ENUMGENERATORS_EMBED_ATTRIBUTES`. This ensures the attributes are embedded in your project
2. Add `compile` to the list of excluded assets in your `` element. This ensures the attributes in your project are referenced, instead of the _NetEscapades.EnumGenerators.Attributes.dll_ library.

Your project file should look something like this:

```xml


Exe
net7.0

$(DefineConstants);NETESCAPADES_ENUMGENERATORS_EMBED_ATTRIBUTES


```

## Preserving usages of the `[EnumExtensions]` attribute

The `[EnumExtensions]` attribute is decorated with the `[Conditional]` attribute, [so their usage will not appear in the build output of your project](https://andrewlock.net/conditional-compilation-for-ignoring-method-calls-with-the-conditionalattribute/#applying-the-conditional-attribute-to-classes). If you use reflection at runtime on one of your `enum`s, you will not find `[EnumExtensions]` in the list of custom attributes.

If you wish to preserve these attributes in the build output, you can define the `NETESCAPADES_ENUMGENERATORS_USAGES` MSBuild variable. Note that this means your project will have a runtime-dependency on _NetEscapades.EnumGenerators.Attributes.dll_ so you need to ensure this is included in your build output.

```xml


Exe
net8.0

$(DefineConstants);NETESCAPADES_ENUMGENERATORS_USAGES



```

## Error CS0436 and [InternalsVisibleTo]

> In the latest version of _NetEscapades.EnumGenerators_, you should not experience error CS0436 by default.

In previous versions of the _NetEscapades.EnumGenerators_ generator, the `[EnumExtensions]` attributes were added to your compilation as `internal` attributes by default. If you added the source generator package to multiple projects, and used the `[InternalsVisibleTo]` attribute, you could experience errors when you build:

```bash
warning CS0436: The type 'EnumExtensionsAttribute' in 'NetEscapades.EnumGenerators\NetEscapades.EnumGenerators\EnumExtensionsAttribute.cs' conflicts with the imported type 'EnumExtensionsAttribute' in 'MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
```

In the latest version of _NetEscapades.EnumGenerators_, the attributes are not embedded by default, so you should not experience this problem. If you see this error, compare your installation to the examples in the installation guide.