{"id":13629544,"url":"https://github.com/Ceiridge/BetterEnums","last_synced_at":"2025-04-17T09:34:42.597Z","repository":{"id":47373933,"uuid":"486973170","full_name":"Ceiridge/BetterEnums","owner":"Ceiridge","description":"Actual C# Enums with values and better performance as a source generator","archived":false,"fork":false,"pushed_at":"2024-03-21T02:08:51.000Z","size":26,"stargazers_count":39,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-01T11:04:45.749Z","etag":null,"topics":["csharp","csharp-sourcegenerator","dotnet","enum","enums","roslyn"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ceiridge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-29T12:58:48.000Z","updated_at":"2024-10-11T08:03:40.000Z","dependencies_parsed_at":"2024-08-01T22:52:04.591Z","dependency_job_id":null,"html_url":"https://github.com/Ceiridge/BetterEnums","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ceiridge%2FBetterEnums","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ceiridge%2FBetterEnums/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ceiridge%2FBetterEnums/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ceiridge%2FBetterEnums/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ceiridge","download_url":"https://codeload.github.com/Ceiridge/BetterEnums/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223751228,"owners_count":17196590,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","enums","roslyn"],"created_at":"2024-08-01T22:01:13.218Z","updated_at":"2024-11-08T20:31:12.570Z","avatar_url":"https://github.com/Ceiridge.png","language":"C#","readme":"# C# Better Enums Source Generator\n\nA step in making C# enums usable in order to not fall behind Java.\\\nA source generator that adds extension methods for enums with any attribute. Also adds more performant methods.\n\nInstall the [NuGet](https://www.nuget.org/packages/BetterEnums) or [GitHub](https://github.com/Ceiridge?tab=packages\u0026repo_name=BetterEnums) package.\n\n## How to use\n```csharp\n[BetterEnum] // Add this attribute to the enum\npublic enum ExampleEnum {\n\tAnExample,\n\n\t// Add attributes to your enum members\n\t[ExampleEnumInfo(\"Hello\", 1)]\n\t[Description(\"Another text\")]\n\tAnother,\n\n\t[Description(\"1\")]\n\tOne\n}\n```\n\nThe following methods are now available via an extension class:\n```csharp\n// Access the value attributes directly\nExampleEnum.Another.GetExampleEnumInfo(); // Retrieves the ExampleEnumInfo attribute value without reflection\nExampleEnum.Another.GetDescription().Description;\n\n// Faster ToString\nExampleEnum.AnExample.BetterToString();\n\n// Programmatically access an attribute (uses slow runtime reflection on each call)\nExampleEnum.One.GetAttributeOfType\u003cDescriptionAttribute\u003e();\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eGenerated source code for one example enum\u003c/summary\u003e\n\n```csharp\n[BetterEnum]\npublic enum ExampleEnum {\n\t[ExampleEnumInfo(\"Example Text\", 123)]\n\t[Description(\"Multiple attributes\")]\n\tExample,\n\t[ExampleEnumInfo(\"Test Enum\", 5)]\n\tTest,\n\t[ExampleEnumInfo(\"Working enum\", 7)]\n\t[Description(\"Even more attributes\")]\n\tWorking\n}\n```\n\n```csharp\nusing System;\nusing System.Reflection;\nnamespace BetterEnumsGen {\n\tpublic static class BetterEnumExtensions {\n\t\tprivate static readonly BetterEnumsExample.ExampleEnumInfoAttribute? attr_24e6ce3758d72003196ef350428e4c1891570e9e5a2ba641ed73cea79a989ad0;\n\t\tprivate static readonly System.ComponentModel.DescriptionAttribute? attr_e770e50ffdea7a63dab5345238ae5f03a375d5a2e505ccf5f4f6baa9f6873a99;\n\t\tprivate static readonly BetterEnumsExample.ExampleEnumInfoAttribute? attr_2cbdc8a4a473f08b7376601fc61353eb382f266e9d2d2856d0921461c9636c8a;\n\t\tprivate static readonly BetterEnumsExample.ExampleEnumInfoAttribute? attr_5cd26a3daff2aa7ab2f9e7f1fa4e397a29f82e0c4a71e4cecf91aa9eb41c94a2;\n\t\tprivate static readonly System.ComponentModel.DescriptionAttribute? attr_feead0a86a3e9dd9da71d5161b6779d6ea88fc67a76495781fe836422a5e714e;\n\n\t\t/// \u003csummary\u003e\n\t\t/// Returns the enum member identifier name (more performant)\n\t\t/// \u003c/summary\u003e\n\t\tpublic static string BetterToString(this BetterEnumsExample.ExampleEnum @enum) {\n\t\t\treturn @enum switch {\n\t\t\t\tBetterEnumsExample.ExampleEnum.Example =\u003e nameof(BetterEnumsExample.ExampleEnum.Example),\n\t\t\t\tBetterEnumsExample.ExampleEnum.Test =\u003e nameof(BetterEnumsExample.ExampleEnum.Test),\n\t\t\t\tBetterEnumsExample.ExampleEnum.Working =\u003e nameof(BetterEnumsExample.ExampleEnum.Working),\n\t\t\t\t_ =\u003e throw new ArgumentOutOfRangeException(nameof(@enum), @enum, null)\n\t\t\t};\n\t\t}\n\n\t\t/// \u003csummary\u003e\n\t\t/// Returns the respective attribute of the given enum.\n\t\t/// May return null if the enum does not have that attribute.\n\t\t/// \u003c/summary\u003e\n\t\tpublic static BetterEnumsExample.ExampleEnumInfoAttribute? GetExampleEnumInfo(this BetterEnumsExample.ExampleEnum @enum) {\n\t\t\treturn @enum switch {\n\t\t\t\tBetterEnumsExample.ExampleEnum.Example =\u003e attr_24e6ce3758d72003196ef350428e4c1891570e9e5a2ba641ed73cea79a989ad0,\n\t\t\t\tBetterEnumsExample.ExampleEnum.Test =\u003e attr_2cbdc8a4a473f08b7376601fc61353eb382f266e9d2d2856d0921461c9636c8a,\n\t\t\t\tBetterEnumsExample.ExampleEnum.Working =\u003e attr_5cd26a3daff2aa7ab2f9e7f1fa4e397a29f82e0c4a71e4cecf91aa9eb41c94a2,\n\t\t\t\t_ =\u003e null\n\t\t\t};\n\t\t}\n\n\n\t\t/// \u003csummary\u003e\n\t\t/// Returns the respective attribute of the given enum.\n\t\t/// May return null if the enum does not have that attribute.\n\t\t/// \u003c/summary\u003e\n\t\tpublic static System.ComponentModel.DescriptionAttribute? GetDescription(this BetterEnumsExample.ExampleEnum @enum) {\n\t\t\treturn @enum switch {\n\t\t\t\tBetterEnumsExample.ExampleEnum.Example =\u003e attr_e770e50ffdea7a63dab5345238ae5f03a375d5a2e505ccf5f4f6baa9f6873a99,\n\t\t\t\tBetterEnumsExample.ExampleEnum.Working =\u003e attr_feead0a86a3e9dd9da71d5161b6779d6ea88fc67a76495781fe836422a5e714e,\n\t\t\t\t_ =\u003e null\n\t\t\t};\n\t\t}\n\n\t\t// Finds all attributes once at runtime\n\t\tstatic BetterEnumExtensions() {\n\t\t\tobject[] attrs_7299f2836dc3779f856acae2b30cad36ea256b0f96ef137f9a8298c8ad88f890 = GetEnumAttributes(typeof(BetterEnumsExample.ExampleEnum));\n\t\t\tattr_24e6ce3758d72003196ef350428e4c1891570e9e5a2ba641ed73cea79a989ad0 = 0 \u003e= attrs_7299f2836dc3779f856acae2b30cad36ea256b0f96ef137f9a8298c8ad88f890.Length ? null : attrs_7299f2836dc3779f856acae2b30cad36ea256b0f96ef137f9a8298c8ad88f890[0] as BetterEnumsExample.ExampleEnumInfoAttribute;\n\t\t\tattr_e770e50ffdea7a63dab5345238ae5f03a375d5a2e505ccf5f4f6baa9f6873a99 = 1 \u003e= attrs_7299f2836dc3779f856acae2b30cad36ea256b0f96ef137f9a8298c8ad88f890.Length ? null : attrs_7299f2836dc3779f856acae2b30cad36ea256b0f96ef137f9a8298c8ad88f890[1] as System.ComponentModel.DescriptionAttribute;\n\t\t\tattr_2cbdc8a4a473f08b7376601fc61353eb382f266e9d2d2856d0921461c9636c8a = 2 \u003e= attrs_7299f2836dc3779f856acae2b30cad36ea256b0f96ef137f9a8298c8ad88f890.Length ? null : attrs_7299f2836dc3779f856acae2b30cad36ea256b0f96ef137f9a8298c8ad88f890[2] as BetterEnumsExample.ExampleEnumInfoAttribute;\n\t\t\tattr_5cd26a3daff2aa7ab2f9e7f1fa4e397a29f82e0c4a71e4cecf91aa9eb41c94a2 = 3 \u003e= attrs_7299f2836dc3779f856acae2b30cad36ea256b0f96ef137f9a8298c8ad88f890.Length ? null : attrs_7299f2836dc3779f856acae2b30cad36ea256b0f96ef137f9a8298c8ad88f890[3] as BetterEnumsExample.ExampleEnumInfoAttribute;\n\t\t\tattr_feead0a86a3e9dd9da71d5161b6779d6ea88fc67a76495781fe836422a5e714e = 4 \u003e= attrs_7299f2836dc3779f856acae2b30cad36ea256b0f96ef137f9a8298c8ad88f890.Length ? null : attrs_7299f2836dc3779f856acae2b30cad36ea256b0f96ef137f9a8298c8ad88f890[4] as System.ComponentModel.DescriptionAttribute;\n\t\t}\n\n\t\t// Taken from https://stackoverflow.com/questions/1799370/getting-attributes-of-enums-value\n\t\t/// \u003csummary\u003e\n\t\t/// Gets an attribute on an enum field value\n\t\t/// \u003c/summary\u003e\n\t\t/// \u003ctypeparam name=\"T\"\u003eThe type of the attribute you want to retrieve\u003c/typeparam\u003e\n\t\t/// \u003cparam name=\"enumVal\"\u003eThe enum value\u003c/param\u003e\n\t\t/// \u003creturns\u003eThe attribute of type T that exists on the enum value\u003c/returns\u003e\n\t\t/// \u003cexample\u003e\u003c![CDATA[string desc = myEnumVariable.GetAttributeOfType\u003cDescriptionAttribute\u003e().Description;]]\u003e\u003c/example\u003e\n\t\tpublic static T? GetAttributeOfType\u003cT\u003e(this Enum enumVal) where T : System.Attribute {\n\t\t\tType type = enumVal.GetType();\n\t\t\tMemberInfo[] memInfo = type.GetMember(enumVal.ToString());\n\t\t\tobject[] attributes = memInfo[0].GetCustomAttributes(typeof(T), false);\n\t\t\treturn (attributes.Length \u003e 0) ? (T)attributes[0] : null;\n\t\t}\n\n\t\tprivate static object[] GetEnumAttributes(Type enumType) {\n\t\t\treturn enumType.GetMembers()\n\t\t\t\t.Where(member =\u003e member.MemberType == MemberTypes.Field \u0026\u0026 ((FieldInfo)member).FieldType == enumType)\n\t\t\t\t.Select(member =\u003e member.GetCustomAttributes(false))\n\t\t\t\t.SelectMany(attributeList =\u003e attributeList)\n\t\t\t\t.ToArray();\n\t\t}\n\t}\n}\n```\n\u003c/details\u003e\n","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"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCeiridge%2FBetterEnums","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCeiridge%2FBetterEnums","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCeiridge%2FBetterEnums/lists"}