C#枚举描述获取
public static class EnumExtension
{
public static string GetDescription(this Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString());
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}
//public static IEnumerable<EnumItem> GetDictionary<TEnum>(this TEnum tEnum)
// where TEnum : struct
//{
// return Enum.GetValues(typeof(TEnum)).OfType<Enum>()
// .Select(x => new EnumItem
// {
// Key = Convert.ToInt32(x),
// Value = x.ToString(),
// Description = x.GetDescription()
// });
//}
public static IEnumerable<EnumItem> GetDictionary(Type tEnum)
{
return Enum.GetValues(tEnum).OfType<Enum>()
.Select(x => new EnumItem
{
Key = Convert.ToInt32(x),
Value = x.ToString(),
Description = x.GetDescription()
});
}
}
C#枚举描述获取的更多相关文章
- .NET--------枚举扩展方法(枚举转list,获取枚举描述)
/// <summary> /// get enum description by name /// </summary> /// <typeparam name=&qu ...
- EnumHelper.cs枚举助手(枚举描述信息多语言支持)C#
C#里面经常会用到枚举类型,枚举是值类型对象,如果你想用枚举类型的多属性特性,或者你想在MVC页面上通过简单的值类型转换,将某字段值所代表的含义转换为文字显示,这时候必须要将枚举扩展,是它支持文本描述 ...
- C#通过反射进行枚举描述相关操作
C#可以通过反射,来获取枚举的描述信息或通过描述信息获取到指定类型的枚举 /// <summary> /// 获取枚举描述 /// </summary> /// <par ...
- 【转载】[C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类
关键代码: using System; using System.Collections; using System.Collections.Generic; using System.Compone ...
- C# 读取枚举描述信息实例
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...
- dotnet core 3.0 swagger 显示枚举描述
上一篇net core 2.2 swagger的枚举描述,core 3.0 需要升级swagger到5.0rc版,配置需要做些修改,swaager启用了OpenApi标准,之前的枚举描述方法也失效了. ...
- dotnet core swagger filter 隐藏接口和显示枚举描述
dotnet core 2.2开发项目中,常会使用Swagger UI来生成在线Api文档. 某些接口不想放到Swagger中可以这样写Filter: /// <summary> /// ...
- C#获取枚举描述代码
public class MusterEnum { /// 获取枚举的描述信息 /// </summary> /// <param name="e">传入枚 ...
- C#取枚举描述
一直都觉得枚举是个很不错的东西,可以给我们带来很多方便,而且也增加代码的可读性. 我在之前已经介绍过枚举的简要应用了,再次再来写下怎么获取枚举的描述. 源码如下: 首先,我们定义个含有描述的枚举类型 ...
随机推荐
- @ResponseBody 返回中文乱码问题解决 spingmvc
<!-- UTF8解决乱码问题 --> <bean class="org.springframework.web.servlet.mvc.method.annotation ...
- Java项目下jar包的放置
build path:引用 web-inf/lib:固定 eclipse编译项目是根据build path的.如果不用eclipse来发布项目的话,就会找不到jar. tomcat运行时首先在它自己的 ...
- ef执行记录原生态代码方法。
select e; var f = (System.Data.Objects.ObjectQuery<SimpleEntry>)final; var s = f.ToTraceString ...
- wdcp的安装扩展模块
其实就是官方包里面的所有附加模块全部支持啦.~~是在官方的基础上修改的优化了每次都解压缩php源码包,按需解压缩使用方法如下wget http://git.oschina.net/loblog/mem ...
- SPRING + THYMELEAF 配置
1.使用的是Spring EL而不是Ognl.2.访问上下文的Bean用${@myBean.doSomething()}3.th:field,th:errors,th:errorclass用于form ...
- Zookeeper全解析——Client端(转)
Zookeeper的Client直接与用户打交道,是我们使用Zookeeper的interface.了解ZK Client的结构和工作原理有利于我们合理的使用ZK,并能在使用中更早的发现问题.本文将在 ...
- C#微信json结构接收参数 转载
http://blog.csdn.net/u010773333/article/details/48524155 发素材的时间要上传资源故此要用json格式数据,需要转化. 微信服务器交互基本上都是j ...
- Codeforces 723c [贪心][乱搞]
/* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给一个n和m. 第二行给n个数. 每次操作可以把n个数中的任何一个数替代为别的数,问最少的操作次数使得1.2.3.4.5...m中的数出现的次数 ...
- Key-Value-Coding(KVC)
Objective-C语法之KVC的使用 除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性. 下图来自苹果官网: 如何使用KVC存 ...
- Runloop基础知识
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...