首先定义一个枚举:两个值:已确认、未确认。

public enum ConfirmStatusEnum
{
[Description("未确认")]
unconfirmed = ,
[Description("已确认")]
confirmed =
}

转换枚举的方法:

private void InitConfirmStatus()
{
int values = ;
object[] atts = null;
ConfirmStatusEntity statusEntity = null;
DescriptionAttribute description = null;
List<ConfirmStatusEntity> status = new List<ConfirmStatusEntity>(); Type type = typeof(ConfirmStatusEnum);//取到枚举的Type
var fields = type.GetFields();//获取枚举中所有字段
foreach (var item in fields)
{
if (item.FieldType != type)//如果类型不是枚举的则跳过
{
continue;
}
statusEntity = new ConfirmStatusEntity();//初始化实体
values = (int)item.GetValue(item.Name);//根据名称获取,枚举项的值
statusEntity.ID = values;
atts = item.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (atts != null && atts.Length != )
{
description = (DescriptionAttribute)atts[];//获取特性的描述信息; description就是特性中的描述信息
//给实体复制
statusEntity.StatusName = description.Description;
status.Add(statusEntity);
}
} //这个地方是我实际情况的处理,供参考
ConfirmStatus = status;
SelectConfirmStatus = status.FirstOrDefault();
}

要转成的实体(根据实际情况,此操作可选):

 public class ConfirmStatusEntity
{
/// <summary>
/// 此项对应枚举的int值,即 0,1 ....
/// </summary>
public int ID { get; set; } /// <summary>
/// 此项对应枚举中特性的具体描述信息
/// </summary>
public string StatusName { get; set; }
}

里面有些是我实际情况使用的实体,供参考。

根据枚举获取枚举的Description特性值的更多相关文章

  1. c#枚举 获取枚举键值对、描述等

    using System; using System.Collections.Generic; using System.Collections.Specialized; using System.C ...

  2. 【转载】[C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类

    关键代码: using System; using System.Collections; using System.Collections.Generic; using System.Compone ...

  3. 获取枚举值上的Description特性说明

    /// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...

  4. 获取枚举Description的Name

    /// <summary> /// 获取枚举Description的Name /// </summary> /// <param name="enumName& ...

  5. 获取枚举Name,Value,Description两种方法

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  6. C# 获取枚举的描述Description

    方法类: public static class EnumExtensions { #region Enum /// <summary> /// 获取枚举变量值的 Description ...

  7. .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法

    一. DescriptionAttribute的普通使用方式 1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显 ...

  8. c#获取枚举

    在实际开发项目中,我们定义了一个枚举,往往我们需要在下拉框或其它地方展示枚举.为了加深印象,也为了帮到有需要的人,我写了一个DEMO. 第一步,我们定义一个枚举: /// <summary> ...

  9. C#获取枚举描述代码

    public class MusterEnum { /// 获取枚举的描述信息 /// </summary> /// <param name="e">传入枚 ...

随机推荐

  1. SEO网站title应该怎么写

    第一:具有独特性 在你的网站中,也许有成千上万的页面,首页-分类-无数的文章页面,这些都有固定的标题,他们的标题最好不要相同.有的时候也许不是`站长们故意的,但是在使用编辑软件的时候,经常 会出现很多 ...

  2. 「小程序JAVA实战」 小程序wxss样式文件的使用(七)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-07/ 细说下微信小程序的wxss样式文件.源码:https://github.com/liming ...

  3. IDE 文件查找快捷键被占

    快捷键 Ctrl+Shift+F 被搜狗输入法抢占了. 打开搜狗输入法里的快捷键设置,去掉,my ide is ok!

  4. Elasticsearch之kopf插件安装之后的浏览详解

    前提, Elasticsearch之插件介绍及安装 https://i.cnblogs.com/posts?categoryid=950999&page=2  (强烈建议,从头开始看) 比如, ...

  5. Linux下安装配置MySQL5.7服务器

    Linux下安装配置MySQL服务器 一.安装环境 ============ OS:centos6.8 MySQL:mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz ...

  6. Spring整合Junit4进行单元测试

    一. 添加依赖包(maven) <dependency> <groupId>junit</groupId> <artifactId>junit</ ...

  7. 浏览器get请求到java后台的值是乱码

     get方式提交的参数编码,只支持iso8859-1编码. 因此,如果里面有中文,在后台就需要转换编码,如下 String zhongwen = request.getParameter(" ...

  8. codeforce469DIV2——C. Zebras

    题意 0, 010, 01010 这一类的01交替且开头和结尾都为0的序列被称为zebra序列.给出一段01序列,尝试能否把他分为k个子序列使得每个子序列都是zebra序列. 分析 这个题应该算是水题 ...

  9. jquery on事件在IE8下失效的一种情况,及解决方法/bootstrap空间绑定控件事件不好用

    同事在复制bootstrap中的select控件之后,发现用$('.selectpicker').selectpicker();刷新下拉框控件不好使,后来发现是用原生js克隆的方法obj.cloneN ...

  10. 643. Maximum Average Subarray I 最大子数组的平均值

    [抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...