Answers for "get enum value from description c#"

C#
1

c# get enum value from string

//This example will parse a string to a Keys value
Keys key = (Keys)Enum.Parse(typeof(Keys), "Space");
//The key value will now be Keys.Space
Posted by: Guest on April-28-2020
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
0

reflection get enum value C#

object underlyingValue = Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType()));
Posted by: Guest on November-02-2021

C# Answers by Framework

Browse Popular Code Answers by Language