C# 反射通过GetCustomAttributes方法,获得自定义特性
http://blog.csdn.net/litao2/article/details/17633107
使用反射访问: 自定义属性的信息和对其进行操作的方法。
一、实例1
1、代码:
如:System.Attribute[] attrs=System.Attribute.GetCustomAttributes(typeof(FirstClass));
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- PrintAuthorInfo(typeof(FirstClass));
- PrintAuthorInfo(typeof(SecondClass));
- PrintAuthorInfo(typeof(ThirdClass));
- Console.ReadKey();
- }
- private static void PrintAuthorInfo(System.Type t)
- {
- System.Console.WriteLine("\n类型的 System.Type 对象是:{0}", t);
- System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t); //反射获得用户自定义属性
- foreach (System.Attribute attr in attrs)
- {
- if (attr is Author)
- {
- Author a = (Author)attr;
- System.Console.WriteLine(" 名称:{0}, 版本: {1:f}", a.GetName(), a.version);
- }
- }
- }
- }
- [System.AttributeUsage(System.AttributeTargets.Class |
- System.AttributeTargets.Struct,
- AllowMultiple = true)
- ]//自定义特性类(应用特性的程序元素(是类或结构),程序元素可以指定多个特性)
- public class Author : System.Attribute
- {
- string name;
- public double version;
- public Author(string name)
- {
- this.name = name;
- version = 1.0; // Default value
- }
- public string GetName()
- {
- return name;
- }
- }
- [Author("H. Ackerman")]
- public class FirstClass
- {
- // ...
- }
- // No Author attribute
- public class SecondClass
- {
- // ...
- }
- [Author("H. Ackerman"), Author("M. Knott", version = 2.0)]
- public class ThirdClass
- {
- //程序元素可以指定多个特性
- }
- }
2、效果:
二、实例2
1、代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Linq.Expressions;
- using System.Collections.Specialized;
- using System.Reflection;
- using System.Data.Linq.Mapping;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- PropertyInfo[] propertys = typeof(FirstClass).GetProperties();//返回FirstClass的所有公共属性
- if (propertys != null && propertys.Length > 0)
- {
- foreach (PropertyInfo p in propertys)
- {
- object[] objAttrs = p.GetCustomAttributes(typeof(ColumnAttribute), true);//获取自定义特性
- //GetCustomAttributes(要搜索的特性类型,是否搜索该成员的继承链以查找这些特性)
- if (objAttrs != null && objAttrs.Length > 0)
- {
- ColumnAttribute attr = objAttrs[0] as ColumnAttribute;
- Console.WriteLine("自定义特性Name:"+p.Name+", 元数据:"+attr);
- }
- };
- }
- Console.ReadKey();
- }
- }
- public class FirstClass
- {
- private int _newsid = 0;
- /// <summary>
- /// 主键
- /// </summary>
- [Column(Name = "NewsId", DbType = "int", IsPrimaryKey = true, CanBeNull = false, IsDbGenerated = true)]
- public int NewsId
- {
- get
- {
- return this._newsid;
- }
- set
- {
- this._newsid = value;
- }
- }
- private string _newsimage = string.Empty;
- /// <summary>
- /// 资讯标题图片
- /// </summary>
- [Column(Name = "NewsImage", DbType = "varchar", IsPrimaryKey = false, CanBeNull = false, IsDbGenerated = false)]
- public string NewsImage
- {
- get
- {
- return this._newsimage;
- }
- set
- {
- this._newsimage = value;
- }
- }
- }
- }
2、效果
其他:
FullName(获得System.Type的完全限定名,包括命名空间)
三、实例3 (设置指定实例 属性 的值)
- FirstClass fClass = new FirstClass();
- PropertyInfo pInstance = typeof(FirstClass).GetProperty("NewsId");//搜索具有指定名称的公共属性
- pInstance.SetValue(fClass, 11, null);//设置指定实例 属性 的值
- Console.WriteLine("新闻ID:"+fClass.NewsId);
- Console.WriteLine("新闻图片:"+fClass.NewsImage);
//在4px的库内操作获取打印机
trv_LabelInvoice.Nodes.Clear();
string strText = string.Empty;
FieldInfo[] fields = typeof(EnumPrintName).GetFields();
foreach (FieldInfo field in fields)
{
strText = field.Name;
object[] arrAttributes = field.GetCustomAttributes(typeof(Attribute), true);
if (arrAttributes != null)
{
EnumAttribute objEnumAttribute = arrAttributes.FirstOrDefault(x => x.GetType().Name.Equals(typeof(EnumAttribute).Name)) as EnumAttribute;
if (objEnumAttribute != null)
{
strText = objEnumAttribute.Description;
}
}
if (trv_LabelInvoice.Nodes.IndexOfKey(field.Name) < 0)
{
trv_LabelInvoice.Nodes.Add(field.Name, strText);
}
}
C# 反射通过GetCustomAttributes方法,获得自定义特性的更多相关文章
- c#通过反射获取类上的自定义特性
c#通过反射获取类上的自定义特性 本文转载:http://www.cnblogs.com/jeffwongishandsome/archive/2009/11/18/1602825.html 下面这个 ...
- .NET C#利用反射获取类文件以及其中的方法&属性 并获取类及方法上的特性
了解C#特性类并声明我们自己的特性类[AttributeTest]代码如下 using System; namespace AttributeTest { /* 特性说明 特性本质是一个继承和使用了系 ...
- Attribute自定义特性+Asp.net MVC中的filter详解
转载自:http://blog.csdn.net/wangyy130/article/details/44241957 一.filter简介 在了解自定义特性前,先引入一个概念filter,它是MVC ...
- 【.net 深呼吸】自定义特性(Attribute)的实现与检索方法
在.net的各个语言中,尤其是VB.NET和C#,都有特性这一东东,具体的概念,大家可以网上查,这里老周说一个非标准的概念——特性者,就是对象的附加数据.对象自然可以是类型.类型成员,以及程序集. 说 ...
- C# winform利用反射和自定义特性加载功能模块(插件式开发)
由于在实际的工作中, 碰见这样的一个问题: 一个软件, 销售给A客户 他需要所有功能, 但是销售给B客户, 他只需要其中的一部分, 1.如果我们在实际的开发过程中, 没有把一些功能模块区分开来的话, ...
- C#反射与特性(七):自定义特性以及应用
目录 1,属性字段的赋值和读值 2,自定义特性和特性查找 2.1 特性规范和自定义特性 2.2 检索特性 3,设计一个数据验证工具 3.1 定义抽象验证特性类 3.2 实现多个自定义验证特性 3.3 ...
- C#提高--------------获取方法返回值的自定义特性(Attribute)
.NET(C#):获取方法返回值的自定义特性(Attribute) 转载 2013年05月08日 10:54:42 1456 来自:http://www.cnblogs.com/mgen/archiv ...
- NPOI+反射+自定义特性实现上传excel转List及验证
1.自定义特性 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public ...
- 反射_IsDefined判断方法上有自定义的标签
在.NET 4.0(当然也包括4.0以前的版本)下,用反射判断某个方法是否运用了自定义Attribute时,可以通过调用MethodInfo的IsDefined()方法进行确认.当然,IsDefine ...
随机推荐
- 你意识到苹果公司已经抛弃了GC吗?
为什么移动Web应用程序很慢(译) - tangzhnju - 博客园 http://www.cnblogs.com/codemood/p/3213459.html
- django实现密码加密的注册(数据对象插入)
在 django实现密码非加密的注册(数据对象插入)的基础上,我们对视图和注册页面进行了简单修改 视图 from django.shortcuts import render,redirect,ren ...
- SqlProfiler的替代品-ExpressProfiler
可以用来跟踪执行的sql语句.安装SqlServer之后SqlServerManagementStudio自带一个SqlProfiler,但是如果安装的SqlExpress,那就没有了. 项目的主页在 ...
- bzoj3007 解救小云公主
3007: 解救小云公主 Time Limit: 5 Sec Memory Limit: 512 MB Submit: 159 Solved: 71 [id=3007" style=&q ...
- if控制器+循环控制器+计数器,控制接口分支
但是我不想这么做,接口只想写一次,让循环控制器和if控制器去判断接口,执行我想要的分支.这里遇到了一个问题,if控制器通过什么去判断接下来的分支?我引入了一个计数器的概念.起始值为0,每次循环加1,将 ...
- SpringBoot入门1—简介及helloworld
Spring Boot简介 Spring Boot让我们的Spring应用变的更轻量化.比如:你可以仅仅依靠一个Java类来运行一个Spring引用.你也可以打包你的应用为jar并通过使用java - ...
- Oracle学习笔记—Db_name、Db_domain、Global_name、Service_name、Instance_name和Oracle_SID(转载)
转载自: Oracle中DB_NAME,SID,DB_DOMAIN,SERVICE_NAME等之间的区别 Db_name:对一个数据库(Oracle database)的唯一标识.这种表示对于单个数据 ...
- Easyui 遮罩实现方式
项目中在提交Ajax请求时,后台处理数据时间有点长,需要一个遮罩,就随便找了一个实现一下:包含两种方式,个人比较喜欢第二种 第一种: $("#saveMaterial").clic ...
- javaweb学习纲要
Java Web 概述 大纲: 1.C/S体系结构 2.B/S体系机构 3.两种体系结构比较 4.主流的Web程序应用平台 5.java web学习路线图 1.C/S体系结构 C/S是Client/ ...
- PHP实现生成唯一编号(36进制的不重复编号)
当我们要将一个庞大的数据进行编号时,而编号有位数限制,比如5位的车牌号.10位的某证件号码.订单流水号.短网址等等,我们可以使用36进制计算出符合位数的不重复的编号. 我们将0-Z(012345678 ...