获取枚举的title
2015-07-14 15:39
393 查看
public class StringValue : System.Attribute { private readonly string _value; public StringValue(string value) { _value = value; } public string Value { get { return _value; } } }
public static class StringEnum { public static string GetStringValue(Enum value) { string output = null; Type type = value.GetType(); FieldInfo fi = type.GetField(value.ToString()); StringValue[] attrs = fi.GetCustomAttributes(typeof (StringValue), false) as StringValue[]; if (attrs != null && attrs.Length > 0) { output = attrs[0].Value; } return output; } }
private enum SignMagnitude { [StringValue("Negative")] Negative = -1, }
使用方法:
StringEnum.GetStringValue(SignMagnitude.Negative);
相关文章推荐
- JAVA 环境变量配置
- CMake + VS2013 Win7x64下编译opencv3.0
- iOS 7中实现模糊效果教程
- LintCode Search a 2D matrix 搜索二维矩阵
- Double-Buffer双缓冲——游戏编程模式(下)
- 解决jsp 文件加载过大的问题
- 【VBA研究】如何将单元格数据赋给数组
- ap6234移植
- 巧用JVM启动参数定位一个问题
- Linux上的常用软件
- sha1加密
- 深入了解java同步、锁紧机构
- Hibernate(四)主键属性
- 百度地图滑动冲突的问题解决
- RecyclerView 的分割线(divider)--DividerItemDecoration
- 全局变量关键字global的使用
- Android应用Activity、Dialog、PopWindow、Toast窗口添加机制及源码分析 《二》
- 协同过滤算法实战
- POJ 2739 Sum of Consecutive Prime Numbers-数论-(连续素数和)
- ARC forbids explicit message send of 'autolease'