RSCG – jos.enumeration

 
 

name jos.enumeration
nuget https://www.nuget.org/packages/jos.enumeration/
link https://github.com/joseftw/jos.enumeration
author Josef Ottosson

Generating enum from static consts

 

This is how you can use jos.enumeration .

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>

  <ItemGroup>
    <PackageReference Include="JOS.Enumeration" Version="4.0.2" />
    <PackageReference Include="JOS.Enumeration.SourceGenerator" Version="4.0.2" />
  </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


// See https://aka.ms/new-console-template for more information
using EnumDemo;

Console.WriteLine("Hello, World!");

var cars= CarTypes.GetAll();
foreach (var car in cars)
{
    Console.WriteLine(car.Description + " - " +car.Value);
}


using JOS.Enumeration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EnumDemo;


partial  record CarTypes : IEnumeration<CarTypes>
{
    public static readonly CarTypes Dacia = new(1, "Dacia");
    public static readonly CarTypes Tesla = new(2, "Tesla");
    public static readonly CarTypes BMW = new(3, "BMW");
    public static readonly CarTypes Mercedes = new(4, "Mercedes");
}


 

The code that is generated is

// <auto-generated>
//     This code was auto generated by JOS.Enumeration.SourceGenerator
// </auto-generated>
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
#if NET8_0_OR_GREATER
using System.Collections.Frozen;
#endif
using JOS.Enumeration;

namespace EnumDemo;
[System.Diagnostics.DebuggerDisplay("{Description}")]
[System.CodeDom.Compiler.GeneratedCode("JOS.Enumeration.SourceGenerator", null)]
partial record CarTypes : IComparable<EnumDemo.CarTypes>
{
    private static readonly IReadOnlySet<EnumDemo.CarTypes> AllItems;
    static CarTypes()
    {
        AllItems = new HashSet<EnumDemo.CarTypes>(4)
        {
            Dacia,
            Tesla,
            BMW,
            Mercedes,
        }.ToFrozenSet();
    }

    private CarTypes(int value, string description)
    {
        Value = value;
        Description = description ?? throw new ArgumentNullException(nameof(description));
    }

    public int Value { get; }
    public string Description { get; }

    public static IReadOnlySet<EnumDemo.CarTypes> GetAll()
    {
        return AllItems;
    }

    public static IEnumerable<EnumDemo.CarTypes> GetEnumerable()
    {
        yield return Dacia;
        yield return Tesla;
        yield return BMW;
        yield return Mercedes;
    }

    public static EnumDemo.CarTypes FromValue(int value)
    {
        return value switch
        {
            1 => Dacia,
            2 => Tesla,
            3 => BMW,
            4 => Mercedes,
            _ => throw new InvalidOperationException($"'{value}' is not a valid value in 'EnumDemo.CarTypes'")};
    }

    public static EnumDemo.CarTypes FromDescription(string description)
    {
        return description switch
        {
            "Dacia" => Dacia,
            "Tesla" => Tesla,
            "BMW" => BMW,
            "Mercedes" => Mercedes,
            _ => throw new InvalidOperationException($"'{description}' is not a valid description in 'EnumDemo.CarTypes'")};
    }

    public static EnumDemo.CarTypes FromDescription(ReadOnlySpan<char> description)
    {
        return description switch
        {
            "Dacia" => Dacia,
            "Tesla" => Tesla,
            "BMW" => BMW,
            "Mercedes" => Mercedes,
            _ => throw new InvalidOperationException($"'{description}' is not a valid description in 'EnumDemo.CarTypes'")};
    }

    public static Type ValueType => typeof(int);

    public int CompareTo(EnumDemo.CarTypes? other) => Value.CompareTo(other!.Value);
    public static implicit operator int (EnumDemo.CarTypes item) => item.Value;
    public static implicit operator EnumDemo.CarTypes(int value) => FromValue(value);
}
// <auto-generated>
//     This code was auto generated by JOS.Enumeration.SourceGenerator
// </auto-generated>
#nullable enable
using System;
using JOS.Enumeration;

namespace EnumDemo;
[System.CodeDom.Compiler.GeneratedCode("JOS.Enumeration.SourceGenerator", null)]
public static class Enumerations
{
    public static class CarTypes
    {
        public static class Dacia
        {
            public const int Value = 1;
            public const string Description = "Dacia";
        }

        public static class Tesla
        {
            public const int Value = 2;
            public const string Description = "Tesla";
        }

        public static class BMW
        {
            public const int Value = 3;
            public const string Description = "BMW";
        }

        public static class Mercedes
        {
            public const int Value = 4;
            public const string Description = "Mercedes";
        }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/jos.enumeration


Posted

in

, ,

by

Tags: