SoFunction
Updated on 2025-03-06

Share the conversion instance of C# enumeration values ​​and names


public static class EnumDemo
    {
        private static string GetName( t, object v)
        {
            try
            {
                return (t, v);
            }
            catch
            {
                return "UNKNOWN";
            }
        }

        /// <summary>
/// Returns the description of the specified value of the specified enumeration type
         /// </summary>
/// <param name="t">Enum Type</param>
/// <param name="v">Enum value</param>
        /// <returns></returns>
        public static string GetDescription( t, object v)
        {
            try
            {
                FieldInfo oFieldInfo = (GetName(t, v));
                DescriptionAttribute[] attributes = (DescriptionAttribute[])(typeof(DescriptionAttribute), false);
                return ( > 0) ? attributes[0].Description : GetName(t, v);
            }
            catch
            {
                return "UNKNOWN";
            }
        }
    }