java 枚举值属性_获取枚举值的属性
我想知道是否可以获取枚举值而不是枚举本身的属性? 例如,假设我有以下枚举:
using System.ComponentModel; // for DescriptionAttribute
enum FunkyAttributesEnum
{
[Description("Name With Spaces1")]
NameWithoutSpaces1,
[Description("Name With Spaces2")]
NameWithoutSpaces2
}
我想要的是枚举类型,产生2个元组的枚举字符串值及其描述。
价值很容易:
Array values = System.Enum.GetValues(typeof(FunkyAttributesEnum));
foreach (int value in values)
Tuple.Value = Enum.GetName(typeof(FunkyAttributesEnum), value);
但是,如何获取描述属性的值以填充Tuple.Desc? 我可以考虑如果Attribute属于枚举本身,那么该怎么做,但是我对如何从枚举的值中获取它感到困惑。
#1楼
您还可以定义一个枚举值,例如Name_Without_Spaces ,当需要描述时,请使用Name_Without_Spaces.ToString().Replace('_', ' ')用空格替换下划线。
#2楼
我实现了此扩展方法,以从枚举值获取描述。 它适用于所有枚举。
public static class EnumExtension
{
public static string ToDescription(this System.Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}
}
#3楼
除了AdamCrawford响应之外 ,我还创建了一种更专业的扩展方法,将其提要以获取描述。
public static string GetAttributeDescription(this Enum enumValue)
{
var attribute = enumValue.GetAttributeOfType();
return attribute == null ? String.Empty : attribute.Description;
}
因此,为了获得描述,您可以使用原始扩展方法作为
string desc = myEnumVariable.GetAttributeOfType().Description
或者您可以简单地将扩展方法调用为:
string desc = myEnumVariable.GetAttributeDescription();
希望可以使您的代码更具可读性。
#4楼
这是从Display属性获取信息的代码。 它使用通用方法检索属性。 如果找不到该属性,它将枚举值转换为字符串,而pascal / camel大小写转换为标题大小写( 在此处获得代码)
public static class EnumHelper
{
// Get the Name value of the Display attribute if the
// enum has one, otherwise use the value converted to title case.
public static string GetDisplayName(this TEnum value)
where TEnum : struct, IConvertible
{
var attr = value.GetAttributeOfType();
return attr == null ? value.ToString().ToSpacedTitleCase() : attr.Name;
}
// Get the ShortName value of the Display attribute if the
// enum has one, otherwise use the value converted to title case.
public static string GetDisplayShortName(this TEnum value)
where TEnum : struct, IConvertible
{
var attr = value.GetAttributeOfType();
return attr == null ? value.ToString().ToSpacedTitleCase() : attr.ShortName;
}
///
/// Gets an attribute on an enum field value
///
/// The enum type
/// The type of the attribute you want to retrieve
/// The enum value
/// The attribute of type T that exists on the enum value
private static T GetAttributeOfType(this TEnum value)
where TEnum : struct, IConvertible
where T : Attribute
{
return value.GetType()
.GetMember(value.ToString())
.First()
.GetCustomAttributes(false)
.OfType()
.LastOrDefault();
}
}
这是用于转换为标题大小写的字符串的扩展方法:
///
/// Converts camel case or pascal case to separate words with title case
///
///
///
public static string ToSpacedTitleCase(this string s)
{
//https://stackoverflow.com/a/155486/150342
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
return textInfo
.ToTitleCase(Regex.Replace(s,
"([a-z](?=[A-Z0-9])|[A-Z](?=[A-Z][a-z]))", "$1 "));
}
#5楼
这应该做您需要的。
var enumType = typeof(FunkyAttributesEnum);
var memberInfos = enumType.GetMember(FunkyAttributesEnum.NameWithoutSpaces1.ToString());
var enumValueMemberInfo = memberInfos.FirstOrDefault(m => m.DeclaringType == enumType);
var valueAttributes =
enumValueMemberInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
var description = ((DescriptionAttribute)valueAttributes[0]).Description;
java 枚举值属性_获取枚举值的属性相关推荐
- java 调用linux 脚本并获取返回值
大家好,我是烤鸭: 今天分享下java 调用 shell脚本 并获取返回值. 代码实践 String cmd = "df -h"; StringBuffer sb = new St ...
- 使用java运行Python脚本并获取返回值
java运行Python脚本并获取返回值 java运行Python脚本并获取返回值 使用runtime类实现在java中运行Python脚本,话不多说,直接上代码 java运行Python脚本并获取返 ...
- java获取radio选中的值_获取radio值
使用jquery获取radio的值 使用jquery获取radio的值,最重要的是掌握jquery选择器的使用,在一个表单中我们通常是要获取被选中的那个radio项的值,所以要加checked来 ...
- bean注入属性_摆脱困境:将属性值注入配置Bean
bean注入属性 Spring Framework对将从属性文件中找到的属性值注入到bean或@Configuration类中提供了很好的支持. 但是,如果将单个属性值注入这些类中,则会遇到一些问题. ...
- python惰性求值效果_让Python中类的属性具有惰性求值的能力
起步 我们希望将一个只读的属性定义为 property 属性方法,只有在访问它时才进行计算,但是,又希望把计算出的值缓存起来,不要每次访问它时都重新计算. 解决方案 定义一个惰性属性最有效的方法就是利 ...
- java jtable逐行遍历_Java获取JTable值(每行)
我想从Jtable中获取值,并且我使用getvalueat尝试了它,但是每当我尝试从JTable获取值时,它只会从所选行的第一列获取值,我需要获取所有值来自我选择的Jtable.你能帮我解决这个问题吗 ...
- mysql某字段值转成大写_获取字段值,并使用MySQL将其转换为大写
让我们首先创建一个表-mysql> create table DemoTable1897 ( Name varchar(20) ); 使用插入命令在表中插入一些记录-mysql> inse ...
- 超链接的目标属性值意义_超链接a的target属性
标签的target意思很明确就是在哪里打开目标文档. 第一种情况: 默认情况:当我们没有设置超链接属性target的value值时默认是_self,即.它使目标文档显示在超链接所在框架或者窗口中.所以 ...
- c# uri 取文件名_C# System.Uri类_获取Url的各种属性_文件名_参数_域名_端口等等
System.Uri类用于处理Uri地址信息,常用到它的地方有,相对Uri地址转绝对Uri地址,获取Uri的某部分信息等等,可以说是一个非常有用的类. 一.属性 AbsolutePath 获取 URI ...
最新文章
- Kettle使用_17 计算器生成时间维度数据
- tensorflow 多人_使用TensorFlow2.x进行实时多人2D姿势估计
- java开发环境搭建 pdf_01搭建java web开发环境.pdf
- 好用的记事本_分类记事本软件哪个好用?大家推荐一个苹果手机用的分类记事本便签呗...
- IDEA快速 实现 SpringMVC 整合xfire 发布 WebService 服务
- 使用poi导出大量数据到excel遇到的问题
- 设置 cell点击 背景色
- 思科DHCP不同网段通信
- 计算机组装内部线的整理,计算机组装与维护笔记整理.doc
- Win32 编程
- matlab经验正交eof,经验正交函数分解(EOF).pdf
- 网易云音乐api,网络太拥挤,登录失败
- 当潮流突破次元空间,你能想象吗?欢迎来到一个叫“人物动漫化”的程序
- 丽博版魔都家居图鉴:如何住进《三十而已》的精致家
- linux中文件夹的作用
- thinkphp使用ajax、jquery、Mysql实现了简单的客户端通信功能
- Django实现视频播放
- vue 使用three.js 实现3D渲染
- 【youcans 的 OpenCV 学习课】7. 空间域图像滤波
- IDEA 鼠标选中是一个矩形区域