C# Enum with readable string

public enum MerchantType       {
            [Description("Master merchant")]
            mastermerchant,
            [Description("Merchant")]
            merchant,
            [Description("Terminal")]
            terminal,
           
            error,
        }



public static string GetDescription(this Enum value)
{
    if (value == null)
    {
        throw new ArgumentNullException("value");
    }

    string description = value.ToString();
    FieldInfo fieldInfo = value.GetType().GetField(description);
    DescriptionAttribute[] attributes =
       (DescriptionAttribute[])
     fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

    if (attributes != null && attributes.Length > 0)
    {
        description = attributes[0].Description;
    }
    return description;
}

Comments

Popular posts from this blog

Setup Test automation with Selenium and Ruby

Log4Net Different files in the same assembly

Romantic Opera - Kasun Kalhara (Lyrics)