EnumDescription
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic; namespace AttributeMeta
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Enum)]
public class EnumDescription : Attribute
{ private FieldInfo fieldInfo; public EnumDescription(string text, int rank)
{
this.Text = text;
this.Rank = rank;
} /// <summary>
/// 描述枚举值,默认排序为5
/// </summary>
/// <param name="enumDisplayText">描述内容</param>
public EnumDescription(string text)
: this(text, 5) { } public string Text { get; set; }
public int Rank { get; set; } public int Value
{
get
{
return (int)fieldInfo.GetValue(null);
}
}
public string FieldName
{
get
{
return fieldInfo.Name;
}
} #region 对枚举描述属性的解释相关函数
/// <summary>
/// 排序类型
/// </summary>
public enum SortType
{
/// <summary>
///按枚举顺序默认排序
/// </summary>
Default,
/// <summary>
/// 按描述值排序
/// </summary>
DisplayText,
/// <summary>
/// 按排序熵
/// </summary>
Rank
} private static Hashtable cachedEnum = new Hashtable(); /// <summary>
/// 得到对枚举的描述文本
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <returns></returns>
public static string GetEnumText(Type enumType)
{
EnumDescription[] eds = (EnumDescription[])enumType.GetCustomAttributes(typeof(EnumDescription), false);
if (eds.Length != 1) return string.Empty;
return eds[0].Text;
} /// <summary>
/// 获得指定枚举类型中,指定值的描述文本。
/// </summary>
/// <param name="enumValue">枚举值,不要作任何类型转换</param>
/// <returns>描述字符串</returns>
public static string GetFieldText(object enumValue)
{
EnumDescription[] descriptions = GetFieldTexts(enumValue.GetType(), SortType.Default);
foreach (EnumDescription ed in descriptions)
{
if (ed.fieldInfo.Name == enumValue.ToString())
return ed.Text;
}
return string.Empty;
} /// <summary>
/// 得到枚举类型定义的所有文本,按定义的顺序返回
/// </summary>
/// <exception cref="NotSupportedException"></exception>
/// <param name="enumType">枚举类型</param>
/// <returns>所有定义的文本</returns>
public static EnumDescription[] GetFieldTexts(Type enumType)
{
return GetFieldTexts(enumType, SortType.Default);
} /// <summary>
/// 得到枚举类型定义的所有文本
/// </summary>
/// <exception cref="NotSupportedException"></exception>
/// <param name="enumType">枚举类型</param>
/// <param name="sortType">指定排序类型</param>
/// <returns>所有定义的文本</returns>
public static EnumDescription[] GetFieldTexts(Type enumType, SortType sortType)
{
EnumDescription[] descriptions = null;
//缓存中没有找到,通过反射获得字段的描述信息
if (cachedEnum.Contains(enumType.FullName) == false)
{
FieldInfo[] fields = enumType.GetFields();
ArrayList edAL = new ArrayList();
foreach (FieldInfo fi in fields)
{
object[] eds = fi.GetCustomAttributes(typeof(EnumDescription), false);
if (eds.Length != 1) continue;
((EnumDescription)eds[0]).fieldInfo = fi;
edAL.Add(eds[0]);
} cachedEnum.Add(enumType.FullName, (EnumDescription[])edAL.ToArray(typeof(EnumDescription)));
}
descriptions = (EnumDescription[])cachedEnum[enumType.FullName];
if (descriptions.Length <= 0) throw new NotSupportedException("枚举类型[" + enumType.Name + "]未定义属性EnumValueDescription"); //按指定的属性冒泡排序
for (int m = 0; m < descriptions.Length; m++)
{
//默认就不排序了
if (sortType == SortType.Default) break; for (int n = m; n < descriptions.Length; n++)
{
EnumDescription temp;
bool swap = false; switch (sortType)
{
case SortType.Default:
break;
case SortType.DisplayText:
if (string.Compare(descriptions[m].Text, descriptions[n].Text) > 0) swap = true;
break;
case SortType.Rank:
if (descriptions[m].Rank > descriptions[n].Rank) swap = true;
break;
} if (swap)
{
temp = descriptions[m];
descriptions[m] = descriptions[n];
descriptions[n] = temp;
}
}
}
return descriptions;
} /// <summary>
/// 获得枚举类型数据的列表
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <returns>Meta 列表</returns>
public static List<Meta> GetMeta(Type enumType)
{
List<Meta> list = new List<Meta>();
FieldInfo[] fields = enumType.GetFields();
foreach (FieldInfo fi in fields)
{
object[] eds = fi.GetCustomAttributes(typeof(EnumDescription), false);
if (eds.Length != 1)
continue;
((EnumDescription)eds[0]).fieldInfo = fi;
var item = eds[0] as EnumDescription;
Meta meta = new Meta
{
MetaName = item.FieldName,
MetaRank = item.Rank,
MetaText = item.Text,
MetaValue = item.Value
};
list.Add(meta);
}
return list;
} /// <summary>
/// 返回枚举类型数据的哈希表
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <returns>Hashtable</returns>
public static Hashtable GetMetaTable(Type enumType)
{
Hashtable table = new Hashtable();
FieldInfo[] fields = enumType.GetFields();
foreach (FieldInfo fi in fields)
{
object[] eds = fi.GetCustomAttributes(typeof(EnumDescription), false);
if (eds.Length != 1)
continue;
((EnumDescription)eds[0]).fieldInfo = fi;
var item = eds[0] as EnumDescription;
table.Add(item.Value, item.Text);
}
return table;
} /// <summary>
/// 根据枚举值获得枚举文本
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <param name="key">枚举值</param>
/// <returns>Text</returns>
public static object GetMetaValue(Type enumType, int key)
{
object value = null;
Hashtable table = GetMetaTable(enumType);
if (table.Count > 0)
{
value = table[key];
}
return value;
} /// <summary>
/// 根据枚举文本获得枚举值
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <param name="value">枚举文本</param>
/// <returns>Value</returns>
public static object GetMetaKey(Type enumType, string value)
{
object key = null;
Hashtable table = GetMetaTable(enumType);
if (table.Count > 0)
{
foreach (DictionaryEntry de in table)
{
if (de.Value.Equals(value))
{
key = de.Key;
}
}
}
return key;
} #endregion
}
public class Meta
{
public string MetaName { get; set; }
public int MetaValue { get; set; }
public string MetaText { get; set; }
public int MetaRank { get; set; }
}
}
EnumDescription的更多相关文章
- [更新设计]跨平台物联网通讯框架ServerSuperIO 2.0 ,功能、BUG、细节说明,以及升级思考过程!
注:ServerSuperIO 2.0 还没有提交到开源社区,在内部测试!!! 1. ServerSuperIO(SSIO)说明 SSIO是基于早期工业现场300波特率通讯传输应用场景发展.演化而来. ...
- [更新]跨平台物联网通讯框架 ServerSuperIO v1.2(SSIO),增加数据分发控制模式
1.[开源]C#跨平台物联网通讯框架ServerSuperIO(SSIO) 2.应用SuperIO(SIO)和开源跨平台物联网框架ServerSuperIO(SSIO)构建系统的整体方案 3.C#工业 ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 13.中英文版本切换设计
目 录 第十三章 中英文版本切换设计... 2 13.1 不用自带的资源文件的理由... 2 13.2 配置文件... 2 13.3 语言 ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 7.外部接口的设计
目 录 第七章 外部接口的设计... 2 7.1 插件接口... 2 7.2 图形显示接口... 3 7.3 ...
- ASP.NET MVC RenderPartial和Partial的区别
背景:ASP.NET MVC 4.0 @{ Html.RenderPartial(...); } public static void RenderPartial(this HtmlHelper ht ...
- 配置NHibernate将枚举保存为Oracle数据库中的字符串
假设有这样一个枚举: /// <summary> /// 字典项类型 /// </summary> public enum DicItemType { [EnumDescrip ...
- C# Enum,Int,String的互相转换
版权声明:本文为博主原创文章,未经博主允许不得转载. Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用Int32.编程语言通常提供语法来声明由一组已 ...
- 适当使用enum做数据字典 ( .net c# winform csharp asp.net webform )
在一些应用中,通常会用到很多由一些常量来进行描述的状态数据,比如性别(男.女),审核(未审核.已审核)等.在数据库中一般用数字形式来存储,比如0.1等. 不好的做法 经常看到一些应用(ps:最近又看到 ...
- 转-C#让枚举返回字符串
下面的手段是使用给枚举项打标签的方式,来返回字符串 下面分别定义一个属性类,和一个枚举帮助类 [AttributeUsage(AttributeTargets.Field,AllowMultiple ...
随机推荐
- weblogic运维时经常遇到的问题和常用的配置
希望这篇能把weblogic运维时经常遇到的问题.常用的配置汇总到一起. 1.配置jvm参数: 一般在domain启动过程中会看到以下启动的日志信息,如下图所示: 图中红色方框部分为启动weblo ...
- curl如何发送json数据?如何发送form数据?python的restfull又该如何获取这些数据?
1.python使用flask+flask_restfull框架写的api接口,做为服务 2.curl 做为客户端发送数据 from flask import request curl发送json的方 ...
- Pixhawk之姿态解算篇(1)_入门篇(DCM Nomalize)
一.开篇 慢慢的.慢慢的.慢慢的就快要到飞控的主要部分了,飞控飞控就是所谓的飞行控制呗,一个是姿态解算一个是姿态控制,解算是解算,控制是控制,各自负责各自的任务.我也不懂.还在学习中~~~~ 近期看姿 ...
- Odoo10对套件的处理
Odoo10对套件的处理更强, 除了老版本支持的 销售套件, 按组件出货: 现在还增加了 采购套件, 按组件进货 建立 组件产品 KIT 设置 虚件BOM 测试, ...
- python 调用函数时使用星号 *, **
python 调用函数时使用星号 *, ** 调用函数时使用星号 * 或 ** test(*args):* 的作用其实就是把序列 args 中的每个元素,当作位置参数传进去.如果 args 等于 (1 ...
- caffeModels--models-caffes-大全
caffe的伯克利主页:http://caffe.berkeleyvision.org/caffe的github主页:https://github.com/BVLC/caffe caffe的model ...
- iOS移动开发周报-第19期
iOS移动开发周报-第19期 前言 欢迎国内的iOS同行或技术作者向我提交周报线索,线索可以是新闻.教程.开发工具或开源项目,将相关文章的简介和链接在微博上发布并 @唐巧_boy 即可. [摘要]:本 ...
- vim 插件: ctrlp.vim
vim-scripts 里可以搜到这个插件. 安装好了之后,在 vim 的 normal 模式之下按 Ctrl+P 组合键即可弹出搜索窗口. * <f5> 更新目录缓存. * <c- ...
- 怎样高速编译mediatek\operator以下代码
mediatek\operator以下有单独的apk.也有overlay的数据,单独的apk会配置anroid.mk,找到相应的路径直接build. 假设是overlay,则编译原来应用的路径,比如 ...
- PHP date()获取系统时间不对
使用date_default_timezone_set(”)方法; <?php error_reporting(0); date_default_timezone_set('PRC'); hea ...