StaticDictionaries 1.0.0-rc

This is a prerelease version of StaticDictionaries.
There is a newer version of this package available.
See the version list below for details.
dotnet add package StaticDictionaries --version 1.0.0-rc
NuGet\Install-Package StaticDictionaries -Version 1.0.0-rc
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StaticDictionaries" Version="1.0.0-rc" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StaticDictionaries --version 1.0.0-rc
#r "nuget: StaticDictionaries, 1.0.0-rc"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install StaticDictionaries as a Cake Addin
#addin nuget:?package=StaticDictionaries&version=1.0.0-rc&prerelease

// Install StaticDictionaries as a Cake Tool
#tool nuget:?package=StaticDictionaries&version=1.0.0-rc&prerelease

StaticDictionaries πŸ“š

.NET CodeFactor codecov License: MIT NuGet version (SoftCircuits.Silk)

Simple high performance solution for data hardcoding. Smart. Flexible. Powerful.

Powered by source generators. Without using reflection.

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πŸ›Έ

  1. Decorate your enum with [StaticDictionary] attribute and define arguments to generate properties:
[StaticDictionary("Surname", "Age")]
public enum User
  1. Decorate your enum members with [Value] attribute and define member values:
[Value("Brown", 27)]
John = 1
  1. Build project to run source generator and get access to generated methods:
int age = User.John.Age();

Powerful enum management 🦾

Source generator creates some methods in [EnumName]Extensions class for convenient and fast enum management:

  • Length quantity of all enum members.
  • MinId returns min Id().
  • MaxId returns max Id().
  • All() returns an array of all enum members.
  • GetById() returns member or NotSupportedException.

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("In progress", 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] enum members must contain [Value] attribute with arguments.
  • Generator creates two methods by default: Id() with (int)member and Name() with nameof(member) if you do not override them.
  • enum members without [Value] attribute will be ignored.
  • 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 be bool.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.1 2,795 11/19/2022
1.3.1-preview1 141 11/18/2022
1.3.0 335 11/11/2022
1.2.0 3,016 10/1/2022
1.1.0 431 8/10/2022
1.0.0 411 7/10/2022
1.0.0-rc 162 7/3/2022
1.0.0-beta2 131 7/2/2022
1.0.0-beta 247 7/2/2022

First release with main functionality.