How to get enum value by string or int

后端 未结 10 1241
慢半拍i
慢半拍i 2020-12-07 12:43

How can I get the enum value if I have the enum string or enum int value. eg: If i have an enum as follows:

public enum TestEnum
{
    Value1 = 1,
    Value         


        
相关标签:
10条回答
  • 2020-12-07 13:33

    Following is the method in C# to get the enum value by string

    ///
    /// Method to get enumeration value from string value.
    ///
    ///
    ///
    
    public T GetEnumValue<T>(string str) where T : struct, IConvertible
    {
        if (!typeof(T).IsEnum)
        {
            throw new Exception("T must be an Enumeration type.");
        }
        T val = ((T[])Enum.GetValues(typeof(T)))[0];
        if (!string.IsNullOrEmpty(str))
        {
            foreach (T enumValue in (T[])Enum.GetValues(typeof(T)))
            {
                if (enumValue.ToString().ToUpper().Equals(str.ToUpper()))
                {
                    val = enumValue;
                    break;
                }
            }
        }
    
        return val;
    }
    

    Following is the method in C# to get the enum value by int.

    ///
    /// Method to get enumeration value from int value.
    ///
    ///
    ///
    
    public T GetEnumValue<T>(int intValue) where T : struct, IConvertible
    {
        if (!typeof(T).IsEnum)
        {
            throw new Exception("T must be an Enumeration type.");
        }
        T val = ((T[])Enum.GetValues(typeof(T)))[0];
    
        foreach (T enumValue in (T[])Enum.GetValues(typeof(T)))
        {
            if (Convert.ToInt32(enumValue).Equals(intValue))
            {
                val = enumValue;
                break;
            }             
        }
        return val;
    }
    

    If I have an enum as follows:

    public enum TestEnum
    {
        Value1 = 1,
        Value2 = 2,
        Value3 = 3
    }
    

    then I can make use of above methods as

    TestEnum reqValue = GetEnumValue<TestEnum>("Value1");  // Output: Value1
    TestEnum reqValue2 = GetEnumValue<TestEnum>(2);        // OutPut: Value2
    

    Hope this will help.

    0 讨论(0)
  • From SQL database get enum like:

    SqlDataReader dr = selectCmd.ExecuteReader();
    while (dr.Read()) {
       EnumType et = (EnumType)Enum.Parse(typeof(EnumType), dr.GetString(0));
       ....         
    }
    
    0 讨论(0)
  • 2020-12-07 13:35

    There are numerous ways to do this, but if you want a simple example, this will do. It just needs to be enhanced with necessary defensive coding to check for type safety and invalid parsing, etc.

        /// <summary>
        /// Extension method to return an enum value of type T for the given string.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T ToEnum<T>(this string value)
        {
            return (T) Enum.Parse(typeof(T), value, true);
        }
    
        /// <summary>
        /// Extension method to return an enum value of type T for the given int.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T ToEnum<T>(this int value)
        {
            var name = Enum.GetName(typeof(T), value);
            return name.ToEnum<T>();
        }
    
    0 讨论(0)
  • 2020-12-07 13:35

    Simply try this

    It's another way

    public enum CaseOriginCode
    {
        Web = 0,
        Email = 1,
        Telefoon = 2
    }
    
    public void setCaseOriginCode(string CaseOriginCode)
    {
        int caseOriginCode = (int)(CaseOriginCode)Enum.Parse(typeof(CaseOriginCode), CaseOriginCode);
    }
    
    0 讨论(0)
提交回复
热议问题