Answers for "from name of enum get enum"

C#
0

Get Enum Name

//Option 1:
string m = Enum.GetName(typeof(MyEnumClass), value);
//Option 2:
string n = nameof(MyEnumClass.Value);
Posted by: Guest on June-07-2021
0

get enum from enum description

public static int GetEnumFromDescription(string description, Type enumType)
{
    foreach (var field in enumType.GetFields())
    {
        DescriptionAttribute attribute
            = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))as DescriptionAttribute;
        if(attribute == null)
            continue;
        if(attribute.Description == description)
        {
            return (int) field.GetValue(null);
        }
    }
    return 0;
}
Posted by: Guest on September-08-2020

C# Answers by Framework

Browse Popular Code Answers by Language