SmartEnums 1.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package SmartEnums --version 1.1.1
NuGet\Install-Package SmartEnums -Version 1.1.1
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="SmartEnums" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SmartEnums --version 1.1.1
#r "nuget: SmartEnums, 1.1.1"
#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 SmartEnums as a Cake Addin
#addin nuget:?package=SmartEnums&version=1.1.1

// Install SmartEnums as a Cake Tool
#tool nuget:?package=SmartEnums&version=1.1.1

SmartEnums

Info

SmartEnums is a simple library, written in C#.NET, which enables you to enhancing the capabilities of standard enum.

Installation

Either checkout this Github repository or install SmartEnums via NuGet Package Manager.

If you want to use NuGet just search for "SmartEnums" or run the following command in the NuGet Package Manager console:

PM> Install-Package SmartEnums

Usage

SmartEnums works on the basis of attributes, which means that now it becomes possible to store custom data for enumeration fields without resorting to writing classes.

Adding fields

First, let's describe the enumeration we want to work with:

public enum UserSubscription
{
  OneMonth,
  SixMonth, 
  Year,
}

Now using attribute EnumValueAttribute(string key, object value) adding custom information in enumeration:

public enum UserSubscription
{
    [EnumValue("Price", 169.0)]
    [EnumValue("Description", "One month subscribe")]
    [EnumValue("Subscribe period", 31)]
    OneMonth,
    
    [EnumValue("Price", 600.50)]
    [EnumValue("Description", "Six month subscribe")]
    [EnumValue("Subscribe period", 31 * 6)]
    SixMonth, 
    
    [EnumValue("Price", 1000.0)]
    [EnumValue("Description", "One year subscribe")]
    [EnumValue("Subscribe period", 31 * 12)]
    Year,
}
//I know that the date count is incorrect, I just gave an example that you can store any data

Now you add custom fields in enumeration.

An attribute can store not only value types, but also more complex structures.

public enum Gender
{
    [EnumValue("Description", "he/him")]
    Male,
    
    [EnumValue("Description", "she/her")]
    Female,
}

public enum TemplateUser
{
    [EnumValue("Age", 20)]
    [EnumValue("Gender", Gender.Male)]
    Jhon,

    [EnumValue("Age", 25)]
    [EnumValue("Gender", Gender.Male)]
    Claus,

    [EnumValue("Age", 15)]
    [EnumValue("Gender", Gender.Female)]
    Elza
}

Getting value

In order to get the value of a custom field, you need to use the extension method

public static T GetValueOf<T>(this Enum element, string key)

In order not to duplicate the code, let's take the enumeration declared above:

var age = TemplateUser.Claus.GetValueOf<int>("Age");
var genderDescription = TemplateUser.Claus.GetValueOf<Gender>("Gender").GetValueOf<string>("Description");

And now you got values of custom attribute fields.

That's all Folks!

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
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
3.0.2 1,320 10/6/2022
3.0.0 531 3/17/2022
2.1.3 417 2/23/2022
2.1.3-alpha 144 2/23/2022
2.1.2 395 2/23/2022
2.1.1 416 1/20/2022
2.1.0 226 1/10/2022
2.0.0 231 1/6/2022
1.1.1 252 1/2/2022
1.1.0 244 1/2/2022

Update work with exceptions and fix namespaces.