Skip to content

MbSoftLab.StringEnums can help you to decorate Enums with custom Strings

Notifications You must be signed in to change notification settings

mbsoftlab/MbSoftLab.StringEnums

Repository files navigation

MbSoftLab.StringEnums

CodeFactor

Release

BuildFromMaster

Description

MbSoftLab.StringEnums can help you to decorate Enums with custom Strings


Quick Start

Simple StringValues for Enums

 
// The enum 
 public enum MyEnum
 { 
   [StringValue("my-css-class-1")]
   Class1,

   [StringValue("my-css-class-2")]
   Class2
 }

Console.WriteLine(MyEnum.Class1.GetStringValue()); // output: my-css-class-1
Console.WriteLine(MyEnum.Class2.GetStringValue()); // output: my-css-class-2

Custom StringValue Attributes

using System;

namespace MbSoftLab.StringEnums.Demo
{
    public class CssClassAttribute : StringValueAttribute
    {
        public CssClassAttribute(string value) : base(value) { }
    }

    public static class CssClassEnumExtension
    {
        // Your concrete EnumExtension
        public static string GetCssClass(this Enum value)
        {
            return value.GetValueFromAttribute<CssClassAttribute>();
        }
    }
    // The enum 
    public enum MyEnum
    {
        [StringValue("i am a string")]
        [CssClass("my-css-class-1")]
        Class1,

        [CssClass("my-css-class-2")]
        Class2
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(MyEnum.Class1.GetStringValue()); // output: i am a string
            Console.WriteLine(MyEnum.Class1.GetCssClass()); // output: my-css-class-1
            Console.WriteLine(MyEnum.Class2.GetCssClass()); // output: my-css-class-2

            Console.ReadKey();
        }
    }
}

About

MbSoftLab.StringEnums can help you to decorate Enums with custom Strings

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages