RSCG – RapidEnum
| name | RapidEnum |
| nuget | https://www.nuget.org/packages/RapidEnum/ |
| link | https://github.com/hanachiru/RapidEnum |
| author | hanachiru |
Generate enum values without reflection
This is how you can use RapidEnum .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RapidEnum" Version="1.0.2" />
</ItemGroup>
</Project>
The code that you will use is
// See https://aka.ms/new-console-template for more information
using EnumDemo;
Console.WriteLine("Hello, World!");
Console.WriteLine("Car types:" + CarTypesEnumExtensions.GetValues().Count);
var cars = CarTypesEnumExtensions.GetValues();
foreach (var car in cars)
{
Console.WriteLine(car.ToStringFast());
}
namespace EnumDemo;
[RapidEnum.RapidEnum]
public enum CarTypes
{
None,
Dacia ,
Tesla ,
BMW ,
Mercedes ,
}
The code that is generated is
// <auto-generated />
namespace EnumDemo
{
public static partial class CarTypesEnumExtensions
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static string ToStringFast(this global::EnumDemo.CarTypes value)
{
return value switch
{
global::EnumDemo.CarTypes.None => nameof(global::EnumDemo.CarTypes.None),
global::EnumDemo.CarTypes.Dacia => nameof(global::EnumDemo.CarTypes.Dacia),
global::EnumDemo.CarTypes.Tesla => nameof(global::EnumDemo.CarTypes.Tesla),
global::EnumDemo.CarTypes.BMW => nameof(global::EnumDemo.CarTypes.BMW),
global::EnumDemo.CarTypes.Mercedes => nameof(global::EnumDemo.CarTypes.Mercedes),
_ => value.ToString()
};
}
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static bool IsDefined(global::EnumDemo.CarTypes value)
{
return value switch
{
global::EnumDemo.CarTypes.None => true,
global::EnumDemo.CarTypes.Dacia => true,
global::EnumDemo.CarTypes.Tesla => true,
global::EnumDemo.CarTypes.BMW => true,
global::EnumDemo.CarTypes.Mercedes => true,
_ => false,
};
}
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static bool IsDefined(string name)
{
return name switch
{
nameof(global::EnumDemo.CarTypes.None) => true,
nameof(global::EnumDemo.CarTypes.Dacia) => true,
nameof(global::EnumDemo.CarTypes.Tesla) => true,
nameof(global::EnumDemo.CarTypes.BMW) => true,
nameof(global::EnumDemo.CarTypes.Mercedes) => true,
_ => false,
};
}
private static readonly global::System.Collections.ObjectModel.ReadOnlyCollection<global::EnumDemo.CarTypes> CacheValues = new global::System.Collections.ObjectModel.ReadOnlyCollection<global::EnumDemo.CarTypes>(new[]
{
global::EnumDemo.CarTypes.None,
global::EnumDemo.CarTypes.Dacia,
global::EnumDemo.CarTypes.Tesla,
global::EnumDemo.CarTypes.BMW,
global::EnumDemo.CarTypes.Mercedes,
});
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static global::System.Collections.Generic.IReadOnlyList<global::EnumDemo.CarTypes> GetValues() => CacheValues;
private static readonly global::System.Collections.ObjectModel.ReadOnlyCollection<string> CacheNames = new global::System.Collections.ObjectModel.ReadOnlyCollection<string>(new[]
{
nameof(global::EnumDemo.CarTypes.None),
nameof(global::EnumDemo.CarTypes.Dacia),
nameof(global::EnumDemo.CarTypes.Tesla),
nameof(global::EnumDemo.CarTypes.BMW),
nameof(global::EnumDemo.CarTypes.Mercedes),
});
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static global::System.Collections.Generic.IReadOnlyList<string> GetNames() => CacheNames;
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static string GetName(global::EnumDemo.CarTypes value)
{
return value.ToStringFast();
}
private static readonly global::System.Collections.ObjectModel.ReadOnlyCollection<Member> CacheMembers = new global::System.Collections.ObjectModel.ReadOnlyCollection<Member>(new[]
{
new Member(nameof(global::EnumDemo.CarTypes.None), global::EnumDemo.CarTypes.None),
new Member(nameof(global::EnumDemo.CarTypes.Dacia), global::EnumDemo.CarTypes.Dacia),
new Member(nameof(global::EnumDemo.CarTypes.Tesla), global::EnumDemo.CarTypes.Tesla),
new Member(nameof(global::EnumDemo.CarTypes.BMW), global::EnumDemo.CarTypes.BMW),
new Member(nameof(global::EnumDemo.CarTypes.Mercedes), global::EnumDemo.CarTypes.Mercedes),
});
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static global::System.Collections.Generic.IReadOnlyList<Member> GetMembers() => CacheMembers;
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static Member GetMember(global::EnumDemo.CarTypes value)
{
return value.ToMember();
}
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static Member ToMember(this global::EnumDemo.CarTypes value)
{
return new Member(ToStringFast(value), value);
}
public sealed class Member
{
public string Name { get; }
public global::EnumDemo.CarTypes Value { get; }
internal Member(string name, global::EnumDemo.CarTypes value)
{
Name = name;
Value = value;
}
public void Deconstruct(out string name, out global::EnumDemo.CarTypes value)
{
name = Name;
value = Value;
}
}
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static global::EnumDemo.CarTypes Parse(string name, bool ignoreCase = false)
{
if (TryParse(name, out var value, ignoreCase))
{
return value;
}
throw new global::System.ArgumentException($"The value '{name}' is not defined in enum 'global::EnumDemo.CarTypes'.");
}
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static bool TryParse(
string name,
out global::EnumDemo.CarTypes value,
bool ignoreCase = false)
{
return ignoreCase ? TryParseIgnoreCase(name, out value) : TryParse(name, out value);
}
private static bool TryParseIgnoreCase(
string name,
out global::EnumDemo.CarTypes value)
{
switch (name)
{
case not null when name.Equals(nameof(global::EnumDemo.CarTypes.None), global::System.StringComparison.OrdinalIgnoreCase):
value = global::EnumDemo.CarTypes.None;
return true;
case not null when name.Equals(nameof(global::EnumDemo.CarTypes.Dacia), global::System.StringComparison.OrdinalIgnoreCase):
value = global::EnumDemo.CarTypes.Dacia;
return true;
case not null when name.Equals(nameof(global::EnumDemo.CarTypes.Tesla), global::System.StringComparison.OrdinalIgnoreCase):
value = global::EnumDemo.CarTypes.Tesla;
return true;
case not null when name.Equals(nameof(global::EnumDemo.CarTypes.BMW), global::System.StringComparison.OrdinalIgnoreCase):
value = global::EnumDemo.CarTypes.BMW;
return true;
case not null when name.Equals(nameof(global::EnumDemo.CarTypes.Mercedes), global::System.StringComparison.OrdinalIgnoreCase):
value = global::EnumDemo.CarTypes.Mercedes;
return true;
case not null when int.TryParse(name, out var val):
value = (global::EnumDemo.CarTypes)val;
return true;
default:
value = default;
return false;
}
}
private static bool TryParse(
string name,
out global::EnumDemo.CarTypes value)
{
switch (name)
{
case nameof(global::EnumDemo.CarTypes.None):
value = global::EnumDemo.CarTypes.None;
return true;
case nameof(global::EnumDemo.CarTypes.Dacia):
value = global::EnumDemo.CarTypes.Dacia;
return true;
case nameof(global::EnumDemo.CarTypes.Tesla):
value = global::EnumDemo.CarTypes.Tesla;
return true;
case nameof(global::EnumDemo.CarTypes.BMW):
value = global::EnumDemo.CarTypes.BMW;
return true;
case nameof(global::EnumDemo.CarTypes.Mercedes):
value = global::EnumDemo.CarTypes.Mercedes;
return true;
case not null when int.TryParse(name, out var val):
value = (global::EnumDemo.CarTypes)val;
return true;
default:
value = default;
return false;
}
}
private static readonly global::System.Type CacheUnderlyingType = global::System.Enum.GetUnderlyingType(typeof(global::EnumDemo.CarTypes));
public static global::System.Type GetUnderlyingType() => CacheUnderlyingType;
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static string GetEnumMemberValue(this global::EnumDemo.CarTypes value)
{
return value switch
{
global::EnumDemo.CarTypes.None => null,
global::EnumDemo.CarTypes.Dacia => null,
global::EnumDemo.CarTypes.Tesla => null,
global::EnumDemo.CarTypes.BMW => null,
global::EnumDemo.CarTypes.Mercedes => null,
_ => null
};
}
}
}
// <auto-generated/>
namespace RapidEnum
{
[global::System.AttributeUsage(global::System.AttributeTargets.Enum)]
internal sealed class RapidEnumAttribute : global::System.Attribute
{
}
[global::System.AttributeUsage(global::System.AttributeTargets.Class, Inherited = false)]
public sealed class RapidEnumWithTypeAttribute : global::System.Attribute
{
public global::System.Type Type { get; }
public RapidEnumWithTypeAttribute(global::System.Type type)
{
Type = type;
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/RapidEnum