/// <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";
      }
    }

 

相关文章:

  • 2021-12-16
  • 2021-11-06
  • 2022-03-06
  • 2021-09-24
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
猜你喜欢
  • 2021-07-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2019-08-25
  • 2022-12-23
  • 2021-07-29
相关资源
相似解决方案