.net 特性 Attribute
public sealed class RemarkAttribute : Attribute
{
public string Remark { get; set; } // 构造函数
public RemarkAttribute(string remark)
{
this.Remark = remark;
} /// <summary>
/// 获取枚举备注属性
/// </summary>
/// <param name="_enum">枚举类参数</param>
/// <returns>String</returns>
public static string GetEnumRemark(Enum _enum)
{
string result = string.Empty;
Type type = _enum.GetType();
FieldInfo fd = type.GetField(_enum.ToString());
if (fd != null)
{
object[] attrs = fd.GetCustomAttributes(typeof(RemarkAttribute), false);
foreach (RemarkAttribute attr in attrs)
result = attr.Remark;
}
return result;
} /// <summary>
/// 下拉列表绑定枚举属性值
/// </summary>
/// <param name="ddl">DropDownList</param>
/// <param name="_enum">枚举</param>
public static void DataBind(DropDownList ddl, Type _enum)
{
List<StructRemark> list = GetStructRemarkList(_enum);
foreach (StructRemark item in list)
{
ListItem listItem = new ListItem();
listItem = new ListItem(item.FieldText, item.FieldValue.ToString());
ddl.Items.Add(listItem);
}
} /// <summary>
/// 下拉列表绑定枚举属性值(有默认项)
/// </summary>
/// <param name="ddl">DropDownList</param>
/// <param name="_enum">枚举</param>
public static void DataBind(DropDownList ddl, Type _enum, string firstValue, string firstText)
{
List<StructRemark> list = GetStructRemarkList(_enum);
foreach (StructRemark item in list)
{
ListItem listItem = new ListItem();
listItem = new ListItem(item.FieldText, item.FieldValue.ToString());
ddl.Items.Add(listItem);
}
ddl.Items.Insert(0, new ListItem(firstText, firstValue));
} /// <summary>
/// 把枚举转换成DataTable
/// </summary>
/// <param name="_enum">枚举</param>
public static DataTable GetDataTable(Type _enum)
{
DataTable dt = new DataTable();
DataColumn dc = null;
dc = dt.Columns.Add("Value", Type.GetType("System.Int32"));
dc = dt.Columns.Add("Text", Type.GetType("System.String"));
DataRow dr = null;
List<StructRemark> list = GetStructRemarkList(_enum);
foreach (StructRemark item in list)
{
dr = dt.NewRow();
dr["Value"] = item.FieldValue;
dr["Text"] = item.FieldText;
dt.Rows.Add(dr);
}
return dt;
} /// <summary>
/// 获取枚举备注属性集
/// </summary>
/// <param name="_enum">枚举</param>
/// <returns>枚举属性结构</returns>
public static List<StructRemark> GetStructRemarkList(Type _enum)
{
List<StructRemark> list = new List<StructRemark>();
StructRemark model = new StructRemark();
foreach (int value in Enum.GetValues(_enum))
{
model.FieldValue = value;
FieldInfo fd = _enum.GetField(Enum.GetName(_enum, value));
string name = string.Empty;
if (fd == null)
name = "";
object[] attrs = fd.GetCustomAttributes(typeof(RemarkAttribute), false);
foreach (RemarkAttribute attr in attrs)
{
name = attr.Remark;
}
model.FieldText = name;
list.Add(model);
}
return list;
} /// <summary>
/// 枚举属性结构
/// </summary>
public struct StructRemark
{
public int FieldValue;//值
public string FieldText;//键
}
}
特性 Attribute:
公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。
我们简单的总结为:定制特性attribute,本质上是一个类,其为目标元素提供关联附加信息,并在运行期以反射的方式来获取附加信息。具体的特性实现方法,在接下来的讨论中继续深入。
自定义特性 public sealed class RemarkAttribute : Attribute 以Attribute Remark则表示你的特性
.net 特性 Attribute的更多相关文章
- [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute
剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...
- [C#] C# 知识回顾 - 特性 Attribute
C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...
- C# 知识特性 Attribute
C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...
- 区分元素特性attribute和对象属性property
× 目录 [1]定义 [2]共有 [3]例外[4]特殊[5]自定义[6]混淆[7]总结 前面的话 其实attribute和property两个单词,翻译出来都是属性,但是<javascript高 ...
- .Net内置特性Attribute介绍
特性Attribute概述 特性(Attribute)是一种特殊的类型,可以加载到程序集或者程序集的类型上,这些类型包括模块.类.接口.结构.构造函数.方法.字段等,加载了特性的类型称之为特性的目标. ...
- 【点滴积累】通过特性(Attribute)为枚举添加更多的信息
转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...
- 理解特性attribute 和 属性property的区别 及相关DOM操作总结
查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点: ...
- 如何获取类或属性的自定义特性(Attribute)
如何获取类或属性的自定义特性(Attribute) 问题说明: 在ActiveRecord或者其他的ORM等代码中, 我们经常可以看到自定义特性(Attribute)的存在(如下面的代码所示) [Pr ...
- C# 知识特性 Attribute,XMLSerialize,
C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用“反射”查询特性,获取特性集合方 ...
- c#特性attribute:
特性是被编译到metadata中, 是提供给反射用的. 特性attribute:1 什么是attribute,和注释有什么区别 2 声明和使用attribute3 使用attribute完成扩展4 ...
随机推荐
- composer的安装和使用 学习日志
如果你做为一个phper,没有用过composer,那你真的不是一个合格的开发者.那么就来记录一下composer的学习日志 下面分享几个学习源头: composer中文网站:https://www. ...
- Ubuntu 下使用 mutt 和 msmtp 发送 Gmail 邮件
参考: http://www.cnblogs.com/refrag/archive/2012/11/28/2793533.html http://www.habadog. ...
- HDU 1166 敌兵布阵 树状数组小结(更新)
树状数组(Binary Indexed Tree(BIT), Fenwick Tree) 是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有 元素之和,但是每次只能修改一 ...
- Excel开发学习笔记:根据工作表worksheet内容控制按钮的状态
开发环境基于VSTO,具体配置:visual studio 2010,VB .Net,excel 2007,文档级别的定制程序. 在Ribbon工具栏中有2个功能按钮,它们是否可用取决于workshe ...
- Oracle 11G的间隔(INTERVAL)分区
-- Create table create table MS_BIGTABLE_LOG ( record_date DATE, col_1 VARCHAR2(), col_2 VARCHAR2() ...
- list map set 集合的区别
Java中的集合包括三大类,它们是Set.List和Map,它们都处于java.util包中,Set.List和Map都是接口,它们有各自的实现 类.Set的实现类主要有HashSet和TreeSet ...
- List<T>集合使用总结
- leetcode821
vector<int> shortestToChar(string S, char C) { vector<int> V; ; int AYC[N]; ; ; i < S ...
- OGG for sqlserver engryption && insert/delete
OGG for sqlserver engryption && insert/delete 1. 源端操作 1.1 获取key 作为数据库用户密码加密 d:\GoldenGate\gg ...
- ssh免密连接远程服务器
ssh免密连接远程服务器 借助ssky-keygen和ssh-copy-id工具,通过4个简单的步骤实现无需输入密码登录远程Linux主机 1 生成密钥 通过内置的工具生成RSA算法加密的密钥 ssh ...