StaticDictionaries
Simple high performance solution for data hardcoding.
Install / Use
/learn @MMaximus111/StaticDictionariesREADME
StaticDictionaries 📚
Simple high performance solution for data hardcoding. Smart. Flexible. Powerful.
Powered by source generators. Without using reflection. .NET 10 now supported ⚒
StaticDictionaries is a convinient library for data hardcoding in your project.
Just decorate the enum using attributes, the source generator will do the rest for you.
Minimum code and time spent - maximum result and performance. Property management has never been so convenient.
Generated methods for managing enum without reflection will make development pleasant and faster than ever.
Installing StaticDictionaries
StaticDictionaries is available on NuGet.
dotnet add package StaticDictionaries
Install-Package StaticDictionaries
Basic usage by steps🛸
- Decorate your enum with
[StaticDictionary]attribute and define arguments to generate properties:
[StaticDictionary("Surname", "Age")]
public enum User
- Decorate your enum members with
[Value]attribute and define member values:
[Value("Brown", 27)]
John = 1
- Build project to run source generator and get access to generated methods:
int id = User.John.Id();
int age = User.John.Age();
string name = User.John.Name();
string surname = User.John.Surname();
Powerful enum management 🦾
Source generator creates some methods in [EnumName]Extensions class for convenient and fast enum management:
Lengthquantity of all enum members.MinIdreturns minId().MaxIdreturns maxId().All()returns an array of allenummembers.GetById()returns member orNotSupportedException.Json()returns serializedenumin json format.Xml()returns serializedenumin xml format.
Dictionary supported primitive types 🗿
| CLR type | Status |
|------------|-------|
| int |✅|
| bool |✅|
| char |✅|
| double |✅|
| string |✅|
Samples 🤝
Employee dictionary with Name, Age and Active properties:
[StaticDictionary("Name", "Age", "Active")]
public enum Employee
{
[Value("Maxim Jhonson", 18, true)]
Maxim = 1,
[Value("John Jhonson", 23, false)]
John = 2
}
public static void Main()
{
int johnAge = Employee.John.Age();
int maximAge = Employee.Maxim.Age();
string johnName = Employee.John.Name();
string maximName = Employee.Maxim.Name();
}
Source generator creates [EnumName]Extensions class that provide some powerful functionality for managing your enum:
[StaticDictionary("Name", "IsLast")]
public enum Status
{
[Value("New state", false)]
New = 1,
[Value("In progress", false)]
InProgress = 2,
[Value("Completed", true)]
Completed = 3
}
public static void Main()
{
Status status = StatusExtensions.GetById(2);
Status[] statuses = StatusExtensions
.All()
.Where(x => !x.IsLast())
.ToArray();
}
Id() and Name() methods are generated by default if you do not override them.
[StaticDictionary]
public enum Metal
{
[Value]
Gold = 1,
[Value]
Ferrum = 2
}
public static void Main()
{
int goldId = Metal.Gold.Id();
string ferrumName = Metal.Ferrum.Name();
}
Important notes ⚠️
[StaticDictionary]parameter names must not be duplicated.[StaticDictionary]parameter names must use the English alphabet only because they will be generated into methods.- All
[Value]attrubutes must contain such arguments quantity as[StaticDictionary]. - All
[StaticDictionary]enummembers can contain[Value]attribute with arguments. - Generator creates two methods by default:
Id()with(int)memberandName()withnameof(member)if you do not override them. enummembers without[Value]attribute will be ignored.enumproperty names must not be equal to [EnumName]Extensions (c# naming rules for methods).Json()andXml()methods generated only if you markenumwith[JsonSupport]or[XmlSupport].enummembers that use bitwise operators must not contain [Value] attribute.- Parameter types are determined automatically, so all parameters in a sequence must be of the same type.
For example, all of the first types should be
string, and all of the second types should bebool.
Performance 🚀
Despite the convenient format of use, the performance remained at a high level, and thanks to separate methods for working with an Enum without reflection, performance increased significantly in some indicators:
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19044.1766 (21H2)
AMD Ryzen 5 3600, 1 CPU, 12 logical and 6 physical cores
.NET SDK=6.0.301
[Host] : .NET 6.0.6 (6.0.622.26707), X64 RyuJIT [AttachedDebugger]
DefaultJob : .NET 6.0.6 (6.0.622.26707), X64 RyuJIT
| Method | Mean | Error | StdDev | Median | |---------------------------- |------------:|----------:|----------:|------------:| | ClassicAccessToMemberName | 0.0001 ns | 0.0002 ns | 0.0001 ns | 0.0001 ns | | GeneratedAccessToMemberName | 0.0039 ns | 0.0041 ns | 0.0030 ns | 0.0014 ns | | ReflectionGetAllMembers | 562.4409 ns | 3.8498 ns | 3.6011 ns | 562.4710 ns | | GeneratedGetAllMembers | 5.6408 ns | 0.1011 ns | 0.0946 ns | 5.6575 ns | | GetEnumMemberById | 0.0008 ns | 0.0008 ns | 0.0007 ns | 0.0004 ns | | GeneratedGetEnumMemberById | 0.0001 ns | 0.0002 ns | 0.0002 ns | 0.0000 ns |
Related Skills
node-connect
351.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
