{"id":13629575,"url":"https://github.com/leoformaggi/enum-utilities-generator","last_synced_at":"2026-01-14T06:39:13.802Z","repository":{"id":51686136,"uuid":"446479258","full_name":"leoformaggi/enum-utilities-generator","owner":"leoformaggi","description":"A source generator to generate compile-time mapping of enums and description attributes","archived":false,"fork":false,"pushed_at":"2022-08-02T07:10:19.000Z","size":42,"stargazers_count":24,"open_issues_count":2,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-19T10:24:05.915Z","etag":null,"topics":["csharp","csharp-sourcegenerator","dotnet","enum","enumeration","roslyn","sourcegenerator"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leoformaggi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-10T15:31:59.000Z","updated_at":"2025-08-25T13:08:42.000Z","dependencies_parsed_at":"2022-08-22T05:40:34.122Z","dependency_job_id":null,"html_url":"https://github.com/leoformaggi/enum-utilities-generator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/leoformaggi/enum-utilities-generator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leoformaggi%2Fenum-utilities-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leoformaggi%2Fenum-utilities-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leoformaggi%2Fenum-utilities-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leoformaggi%2Fenum-utilities-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leoformaggi","download_url":"https://codeload.github.com/leoformaggi/enum-utilities-generator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leoformaggi%2Fenum-utilities-generator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28412211,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T05:26:33.345Z","status":"ssl_error","status_checked_at":"2026-01-14T05:21:57.251Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","csharp-sourcegenerator","dotnet","enum","enumeration","roslyn","sourcegenerator"],"created_at":"2024-08-01T22:01:13.959Z","updated_at":"2026-01-14T06:39:13.784Z","avatar_url":"https://github.com/leoformaggi.png","language":"C#","funding_links":[],"categories":["Source Generators","Do not want to test 112 ( old ISourceGenerator )"],"sub_categories":["Enums","1. [ThisAssembly](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ThisAssembly) , in the [EnhancementProject](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#enhancementproject) category"],"readme":"# EnumUtilitiesGenerator\n\nA source generator for C# that uses Roslyn to create a small helper class for your Enums, with helpfull mapping between an enum member and its description attribute. By using it you avoid using reflection or boilerplate code to map enums and descriptions.\n\n# Installation\n\nInstall the generator via nuget:\n\n`Install-Package EnumUtilitiesGenerator -Version 0.1.6`\n\n# Benchmark\n\nTo prove the performance benefit, some benchmarks were made and compared. Since enums with few and lots of members behave differently, those scenarios were covered.\nScenarios: \n- A simple enumMember.ToString()\n- Obtaining the DescriptionAttribute value\n- Obtaining the enum from a given DescriptionAttribute value\n\n## Results:\n```\n|                                            Method |           Mean |         Error |        StdDev |         Median |  Gen 0 |  Gen 1 | Allocated |\n|-------------------------------------------------- |---------------:|--------------:|--------------:|---------------:|-------:|-------:|----------:|\n|                    ToString_Native_With_5_Members |      77.695 ns |     7.4842 ns |    22.0672 ns |      85.540 ns | 0.0057 |      - |      24 B |\n|        GetDesriptionFast_Generated_With_5_Members |       2.099 ns |     0.1701 ns |     0.4824 ns |       2.297 ns |      - |      - |         - |\n|                  ToString_Native_With_100_Members |      28.546 ns |     0.5210 ns |     0.8847 ns |      28.761 ns | 0.0057 | 0.0003 |      24 B |\n|      GetDesriptionFast_Generated_With_100_Members |       2.434 ns |     0.0725 ns |     0.0566 ns |       2.433 ns |      - |      - |         - |\n|   GetDesriptionFromEnum_Reflection_With_5_Members |   1,327.510 ns |    10.4239 ns |     9.2405 ns |   1,329.483 ns | 0.0610 |      - |     256 B |\n|    GetDesriptionFromEnum_Generator_With_5_Members |       2.034 ns |     0.1203 ns |     0.2260 ns |       2.064 ns |      - |      - |         - |\n| GetDesriptionFromEnum_Reflection_With_100_Members |   1,381.913 ns |    27.5457 ns |    72.0823 ns |   1,392.869 ns | 0.0610 |      - |     256 B |\n|  GetDesriptionFromEnum_Generator_With_100_Members |       2.617 ns |     0.0500 ns |     0.0443 ns |       2.616 ns |      - |      - |         - |\n|   GetEnumFromDesription_Reflection_With_5_Members |   8,545.634 ns |   140.6773 ns |   162.0042 ns |   8,483.183 ns | 0.3510 |      - |   1,496 B |\n|    GetEnumFromDesription_Generated_With_5_Members |     320.504 ns |     4.4171 ns |     3.9157 ns |     321.430 ns |      - |      - |         - |\n| GetEnumFromDesription_Reflection_With_100_Members | 105,295.058 ns | 2,094.0809 ns | 5,176.0491 ns | 106,267.151 ns | 4.3945 |      - |  18,498 B |\n|  GetEnumFromDesription_Generated_With_100_Members |   4,749.037 ns |    66.6945 ns |    62.3861 ns |   4,729.669 ns |      - |      - |         - |\n```\n\nIt's very clear how the generated code is faster and also does not pressure the garbage collector.\n\n# How to use it\n\nAdd the attribute GenerateHelper to the enums you want to map members and descriptions, like so:\n\n```csharp\n[GenerateHelper(GenerateHelperOption.UseItselfWhenNoDescription)]\npublic enum PaymentMethod\n{\n    [Description(\"Credit card\")]\n    Credit,\n    [Description(\"Debit card\")]\n    Debit,\n    Cash\n}\n```\n\nThat will generate a helper class with 2 methods with compile-time mapping, for each enum found in the consuming project with the GenerateHelper attribute, and an extra method to return all available descriptions.\n\nThe generated code:\n\n```csharp\npublic static class PaymentMethodHelper\n{\n    private static readonly string[] _descriptions = new string[]\n    {\n        \"Credit card\",\n        \"Debit card\",\n    };\n\n    public static string[] GetAvailableDescriptions()\n    {\n        return _descriptions;\n    }\n\n    public static string GetDescriptionFast(this PaymentMethod @enum)\n    {\n        return @enum switch\n        {\n            PaymentMethod.Credit =\u003e \"Credit card\",\n            PaymentMethod.Debit =\u003e \"Debit card\",\n            PaymentMethod.Cash =\u003e nameof(PaymentMethod.Cash)\n        };\n    }\n\n    public static PaymentMethod? GetEnumFromDescriptionFast(string description)\n    {\n        return GetEnumFromDescriptionFast(description, StringComparison.InvariantCultureIgnoreCase);\n    }\n\n    public static PaymentMethod? GetEnumFromDescriptionFast(string description, StringComparison stringComparison)\n    {\n        return description switch\n        {\n            _ when string.Equals(\"Credit card\", description, stringComparison) =\u003e PaymentMethod.Credit,\n            _ when string.Equals(\"Debit card\", description, stringComparison) =\u003e PaymentMethod.Debit,\n            _ when string.Equals(\"Cash\", description, stringComparison) =\u003e PaymentMethod.Cash,\n            _ =\u003e null\n        };\n    }\n}\n```\n\n# Behaviour\n\nEach member will be mapped to and from its Description value. Members without the attribute or with an empty attribute will map according to the option chosen:\n\n- IgnoreEnumWithoutDescription: Returns null\n- ThrowForEnumWithoutDescription: throws an InvalidOperationException.\n- UseItselfWhenNoDescription: will map using the member name. Equivalent to nameof(EnumType.MemberX) or EnumType.MemberX.ToString().\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleoformaggi%2Fenum-utilities-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleoformaggi%2Fenum-utilities-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleoformaggi%2Fenum-utilities-generator/lists"}