.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 ...
随机推荐
- Scanner 的练习 。。。。依然不懂用法。。。苦恼
package com.b; import java.util.Random; import java.util.Scanner; public class Core { public static ...
- EasyUI TreeJson
1. TreeJson str = GetTreeJsonByTable(dt, "); StringBuilder treeResult = new StringBuilder(); St ...
- Mongodb时间问题
Java保存到mongodb当前时间,使用RoboMongo查看数据显示时间比当前时间少8个小时,这是客户端的问题. MongoDB中的Date类型数据只保存绝对时间值,不保存时区信息,因此“显示的时 ...
- PHP面向对象深入研究之【了解类】与【反射API】
了解类 class_exists验证类是否存在 <?php // TaskRunner.php $classname = "Task"; $path = "task ...
- python开发面向对象基础:接口类&抽象类&多态&钻石继承
一,接口类 继承有两种用途: 一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某基类,定义一个接口类Interface,接口类中定义了一些接口名(就是函数名)且并未实 ...
- 使用Docker模拟ansible集群环境
/etc/ansible/hosts 192.168.99.100 ansible_ssh_port=8081 ansible_ssh_user=root 配置容器免密码SSH登录
- BugkuCTF 逆向
Easy_vb 下载文件,打开 用 IDA 或 OD 打开 在中间部分发现 flag 把 MCTF 改成 flag,就是正确的 flag 了 Easy_Re 下载附件,打开
- FatMouse' Trade(Hdu 1009)
Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the wareho ...
- Python代码的编译
Python代码的编译 Python代码在解释执行之前,是会被编译成.pyc或者.pyo文件的,它们是中间字节码表示的文件,之后Python虚拟机才会去解释执行它们. 1.pyc文件 ======== ...
- CString,string和char*
CString是MFC中的 标准C中没有string,有string.h头文件,其中是strcpy,strcmp等函数.但操作对象都是char*类型 string是C++中封装的 转化:LPCSTR ...