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的更多相关文章

  1. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  2. [C#] C# 知识回顾 - 特性 Attribute

    C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...

  3. C# 知识特性 Attribute

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...

  4. 区分元素特性attribute和对象属性property

    × 目录 [1]定义 [2]共有 [3]例外[4]特殊[5]自定义[6]混淆[7]总结 前面的话 其实attribute和property两个单词,翻译出来都是属性,但是<javascript高 ...

  5. .Net内置特性Attribute介绍

    特性Attribute概述 特性(Attribute)是一种特殊的类型,可以加载到程序集或者程序集的类型上,这些类型包括模块.类.接口.结构.构造函数.方法.字段等,加载了特性的类型称之为特性的目标. ...

  6. 【点滴积累】通过特性(Attribute)为枚举添加更多的信息

    转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...

  7. 理解特性attribute 和 属性property的区别 及相关DOM操作总结

    查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点:  ...

  8. 如何获取类或属性的自定义特性(Attribute)

    如何获取类或属性的自定义特性(Attribute) 问题说明: 在ActiveRecord或者其他的ORM等代码中, 我们经常可以看到自定义特性(Attribute)的存在(如下面的代码所示) [Pr ...

  9. C# 知识特性 Attribute,XMLSerialize,

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用“反射”查询特性,获取特性集合方 ...

  10. c#特性attribute:

    特性是被编译到metadata中,  是提供给反射用的. 特性attribute:1 什么是attribute,和注释有什么区别 2 声明和使用attribute3 使用attribute完成扩展4 ...

随机推荐

  1. ubuntu14.04安装pyspider

    sudo apt-get install libcurl4-openssl-dev libxml2-dev libxslt1-dev sudo atp-get install phantomjs 激活 ...

  2. OBS第三方推流直播教程

    第三方推流使用场景 1.当使用YY客户端进行直播遇到问题,暂无解决方法的时候,可以使用第三方直播软件OBS进行推流. 2.对OBS情有独钟的主播. OBS简介: OBS是一款比较好用的开源直播软件,目 ...

  3. Redis数据清除问题

    Redis中数据清除可以分为两种方式 手动清除:指定要清除的key,通过delete命令即可清除 自动清除:使用Redis提供的数据过期策略 Redis数据过期策略      redis提供了非常灵活 ...

  4. 水仙花之java与c++的战争======

    总结:同样在C++里可以运行正常的水仙花,在java里不行 为什么??是运算符优先级的问题吗: package com.a; //水仙花数 一个三位数 324:426/195 public class ...

  5. codeforce 985A Chess Placing(暴力)

    Chess Placing time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. java成神之——java常识

    java常识 简单DOS命令 java基础名词 java编译和运行流程 Eclipse常用快捷键 Eclipse相关操作 java基本数据类型 java基本数据类型转换 java常用运算符 java流 ...

  7. 【面试】D

    昨天去了慕名已久的Dell面试(Dell自2015年退出了世界500强的评比),一面基本合格,二面基本没答上... 对公司的整体印象非常好(每个人桌上都有两台很大的显示器:9:00-15:30,如果能 ...

  8. win7下cygwin 中 root用户的设置

    问题描述: cygwin 在 win10下安装完成后使用当前用户登录后看所在磁盘的文件权限是没有问题的,但在cygwin编译出来的文件的权限为空,这个问题可以使用以下方法来解决: 解决办法: 将cyg ...

  9. HTTP协议头域详解

    HTTP协议头域详解 Requests部分 Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/html Accept-Charset 浏览器可以接受的字符编 ...

  10. leetcode693

    class Solution { public: bool hasAlternatingBits(int n) { ; while (n) { ; ) { last = x; } else { if ...