FastEnumToString 2.0.0-beta
dotnet add package FastEnumToString --version 2.0.0-beta
NuGet\Install-Package FastEnumToString -Version 2.0.0-beta
<PackageReference Include="FastEnumToString" Version="2.0.0-beta"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="FastEnumToString" Version="2.0.0-beta" />
<PackageReference Include="FastEnumToString"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add FastEnumToString --version 2.0.0-beta
#r "nuget: FastEnumToString, 2.0.0-beta"
#:package FastEnumToString@2.0.0-beta
#addin nuget:?package=FastEnumToString&version=2.0.0-beta&prerelease
#tool nuget:?package=FastEnumToString&version=2.0.0-beta&prerelease
FastEnumToString
Source Generator generating ToString extension methods for any enums.
Configure
Global configuration:
<PropertyGroup>
<FastEnumDefaultBehaviour>Default</FastEnumDefaultBehaviour>
</PropertyGroup>
Or you can use the [ToString] attribute to override the global configuration by calling its parameterized constructor:
[ToString(ToStringDefault.First)]
Available values (for both):
- Default: Same as
Throw - First: uses the first avalilable enum member in the
switch's default arm, - Throw: throwing an
ArgumentOutOfRangeException
*the csproj configuration is case insensitive
Example
using FastEnumToString;
Color color = (Color)5;
Console.WriteLine(color);
Console.WriteLine(color.FastToString());
Console.WriteLine(Asd<int, List<int>>.FFF.None.FastToString());
Console.WriteLine(EnumStringConverter.FastToString(Asd<int, List<int>>.FFF.None));
namespace MyNamespace
{
public class NestingClass<T, K>
where T : struct
where K : class, new()
{
[ToString]
public enum NestedInClassEnum
{
None
}
}
[ToString(ToStringDefault.First)]
public enum Color
{
Red,
Green,
Blue,
}
[Flags] // Flags are not yet supported
public enum Options
{
ToString = 1,
Parse = 2,
HasFlag = 4,
}
}
The followning will be generated:
// <auto-generated/>
namespace ToStringExample
{
[global::System.CodeDom.Compiler.GeneratedCode("FastEnumToString", "2.0.0")]
public static class EnumStringConverter
{
public static string FastToString<T, K>(this MyNamespace.NestingClass<T, K>.NestedInClassEnum enumValue)
where T : struct
where K : class, new()
=> enumValue switch
{
MyNamespace.NestingClass<T, K>.NestedInClassEnum.None => nameof(MyNamespace.NestingClass<T, K>.NestedInClassEnum.None),
_ => throw new global::System.ArgumentOutOfRangeException(nameof(enumValue), enumValue, $"Value: '{(int)enumValue}' cannot be found in the provided enum type!")
};
public static string FastToString(this MyNamespace.Color enumValue) => enumValue switch
{
MyNamespace.Color.Red => nameof(MyNamespace.Color.Red),
MyNamespace.Color.Green => nameof(MyNamespace.Color.Green),
MyNamespace.Color.Blue => nameof(MyNamespace.Color.Blue),
_ => nameof(MyNamespace.Color.Red)
};
}
}
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|
Generator is reimplemented as an incremental generator. From now users must opt-in for FastToString method generation!