获取枚举类型Description特性的描述信息

C#中可以对枚举类型用Description特性描述。

如果需要对Description信息获取,那么可以定义一个扩展方法来实现。代码如下:

    public static class EnumExtensions
    {
        public static string GetDescription(this object value)
        {
            if (value==null)
                return string.Empty;

            Type type = value.GetType();
            var fieldInfo = type.GetField(Enum.GetName(type, value));
            if(fieldInfo!=null)
            {
                if(Attribute.IsDefined(fieldInfo,typeof(DescriptionAttribute)))
                {
                    var description =
                        Attribute.GetCustomAttribute(fieldInfo, typeof (DescriptionAttribute)) as DescriptionAttribute;

                    if(description!=null)
                        return description.Description;
                }
            }
            return string.Empty;
        }
    }
时间: 2024-08-02 23:07:45

获取枚举类型Description特性的描述信息的相关文章

从一个int值显示相应枚举类型的名称或者描述

我正在做一个出入库管理的简单项目,在Models里定义了这样的枚举类型 public enum InOrOut { [Description("出库")] Out = 0, [Description("入库")] In = 1 } 我想在输入参数为数据库字段值1或者0的时候,在页面上显示为枚举Name:In.Out,或者干脆显示为Description:出库.入库. 获取枚举Name其实很简单: return Enum.GetName(typeof(InOrOut)

简析Geoserver中获取图层列表以及各图层描述信息的三种方法

1.背景 实际项目中需要获取到Geoserver中的图层组织以及各图层的描述信息:比如字段列表等.在AGS中,我们可以直接通过其提供的REST服务获取到图层组织情况以及图层详细信息列表,具体如下所示: 那么在Geoserver中是否也有相关用法?各种方法之间有何优劣? 2.REST请求方法 2.1方法描述 该方法与上面讲解的AGS的REST请求方法类似,也是先获取到组织情况然后再进行各个图层的描述信息获取: 获取workspace信息: 获取workspace下的datasource信息:获取w

获取枚举类型的描述description

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace candel { class Program { static void Main(string[] args) { int x = (in

反射:获取枚举类型的Name,Value,Description

[Obsolete("请使用新的方法XXX")] //使用Obsolete特性来告诉使用者这是一个过期的方法 private static void Test() { Type t = typeof(ApprovalStatus); var fields = t.GetFields(); foreach (var item in fields) { if (!item.IsSpecialName) { var attr = item.GetCustomAttribute(typeof(

获取枚举类型描述

代码修改前: switch (months) { case "1": nums = "1"; break; case "2": nums = "2"; break; case "3": nums = "3"; break; case "4": nums = "4"; break; case "5": nums = "5

获取根据枚举名称获取枚举描述

/// <summary> /// 获取根据枚举名称获取枚举描述 /// </summary> /// <typeparam name="T">枚举类型</typeparam> /// <param name="enumName">枚举名称</param> /// <returns></returns> public static string GetEnumDescri

.NET枚举类型转为List类型

如图所示这个竞卖状态,原先是在前端界面通过html代码写死的几个状态,现在需要改为动态加载.这个几个状态是定义的枚举类型. 1:定义一个枚举类型 /// <summary>    /// 资源状态    /// </summary>    public enum ResourceState    {        /// <summary>        /// 下架        /// </summary>        [Description(&qu

C#记录日志、获取枚举值 等通用函数列表

using System;using System.Collections;using System.Collections.Generic;using System.ComponentModel;using System.Linq; namespace System.Web.Mvc{    #region 通用函数    /// <summary>    /// 通用函数    /// </summary>    public class Common    {        #

向Java枚举类型中添加新方法

除了不能继承enum之外,可将其看做一个常规类.甚至可以有main方法. 注意:必须先定义enum实例,实例的最后有一个分号. 下面是一个例子:返回对实例自身的描述,而非默认的toString返回枚举实例的名字. public enum Color { RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4); // 成员变量 private String nam