zoukankan      html  css  js  c++  java
  • 枚举、反射等 GetEnumName GetEnumDescription

    /// <summary>
        /// Retrieves the name of the constant in the specified enumeration that has the specified value.
        /// </summary>
        /// <param name="t"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public static string GetEnumName(System.Type t, object v)
        {
          try
          {
            return Enum.GetName(t, v);
          }
          catch
          {
            return "Unknown";
          }
        }
    
        /// <summary>
        /// Get specified description of the specified enum
        /// </summary>
        /// <param name="t"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public static string GetEnumDescription(System.Type t, object v)
        {
          try
          {
            FieldInfo fi = t.GetField(GetEnumName(t, v));
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : GetEnumName(t, v);
          }
          catch
          {
            return "Unknown";
          }
        }
  • 相关阅读:
    敌兵布阵
    Points on Cycle
    Hero
    E~最少拦截系统
    C
    A
    J
    H
    G
    A
  • 原文地址:https://www.cnblogs.com/jasonlny/p/3684657.html
Copyright © 2011-2022 走看看