C#特性与反射机制
2016-03-05 12:43
639 查看
C#特性与反射机制
大纲
特性预定义特性
AttributeUsage
Conditional
Obsolete
自定义特性
反射机制
特性
什么是特性?特性(Attribute)用于添加元数据,如编译器指令和注释、描述、方法、类等其他信息。
特性的语法
[attribute(positional_parameters, name_parameter = value, ...)] element
特性(Attribute)的名称和值是在方括号内规定的,放置在它所应用的元素之前。
positional_parameters规定必需的信息,
name_parameter规定可选的信息。
预定义特性
AttributeUsage
预定义特性 AttributeUsage 描述了如何使用一个自定义特性类。它规定了特性可应用到的项目的类型。语法
[AttributeUsage( validon, AllowMultiple=allowmultiple, Inherited=inherited )]
其中,
参数
validon规定特性可被放置的语言元素。它是枚举器
AttributeTargets的值的组合。默认值是
AttributeTargets.All。
参数
allowmultiple(可选的)为该特性的
AllowMultiple属性(property)提供一个布尔值。如果为 true,则该特性是多用的。默认值是 false(单用的)。
参数
inherited(可选的)为该特性的
Inherited属性(property)提供一个布尔值。如果为 true,则该特性可被派生类继承。默认值是 false(不被继承)。
例如,
[AttributeUsage(AttributeTargets.Class)] class SthAttribute: Attribute{} //自定义特性
Conditional
这个预定义特性标记了一个条件方法,其执行依赖于它顶的预处理标识符。语法
[Conditional( conditionalSymbol )]
Obsolete
这个预定义特性标记了一个条件方法,其执行依赖于它顶的预处理标识符。语法
[Obsolete( message )] [Obsolete( message, iserror )]
其中,
- 参数
message,是一个字符串,描述项目为什么过时的原因以及该替代使用什么。
- 参数
iserror,是一个布尔值。如果该值为 true,编译器应把该项目的使用当作一个错误。默认值是 false(编译器生成一个警告)。
创建自定义特性
创建并使用自定义特性包含四个步骤:声明自定义特性
构建自定义特性
在目标程序元素上应用自定义特性
通过反射访问特性
声明自定义特性例子,
public class TableAttribute: Attribute{} //特性必须是Attribute的派生类
构建自定义特性例子,
public class TableAttribute: Attribute{ public string TableName{ get; set; } ... }
在目标程序元素上应用自定义特性例子,
[TableAttribute(TableName="department")] public class Department{ //... }
通过反射访问特性
例子,
var obj = new Department(); #region process ... #endregion var ta = Attribute.GetCustomAttribute(obj.GetType(), typeof(TableAttribute)) as TableAttribute;
反射机制
什么是反射?反射(Reflection) 对象用于在运行时获取类型信息。
反射(Reflection)有下列用途:
- 它允许在运行时查看属性(attribute)信息。
- 它允许审查集合中的各种类型,以及实例化这些类型。
- 它允许延迟绑定的方法和属性(property)。
- 它允许在运行时创建新类型,然后使用这些类型执行一些任务。
查看属性
NewClassw nc = new NewClassw(); Type t = nc.GetType(); // get the public properties PropertyInfo[] pis = t.GetProperties(); foreach(PropertyInfo pi in pis) { Console.WriteLine(pi.Name); //output the public property name } // get the public methods MethodInfo[] mis = t.GetMethods(); foreach (MethodInfo mi in mis) { Console.WriteLine(mi.ReturnType+" "+mi.Name); }
实例化
NewClassw nc = new NewClassw(); Type t = nc.GetType(); object obj = Activator.CreateInstance(t); //取得ID字段 FieldInfo fi = t.GetField("ID"); //给ID字段赋值 fi.SetValue(obj, "k001"); //取得MyName属性 PropertyInfo pi1 = t.GetProperty("MyName"); //给MyName属性赋值 pi1.SetValue(obj, "grayworm", null); PropertyInfo pi2 = t.GetProperty("MyInfo"); pi2.SetValue(obj, "hi.baidu.com/grayworm", null); //取得show方法 MethodInfo mi = t.GetMethod("show"); //调用show方法 mi.Invoke(obj, null);
延迟绑定的方法和属性
Student nc = new Student(); Type t= nc.GetType(); //获取NewClass所有属性 PropertyInfo[] pis = t.GetProperties(); //从数据库中获取Student表 DataTable dt = SqlHelper.ExecuteDataTable("SELECT * FROM Student WHERE ID=@ID", new SqlParameter("@ID", studentID)); foreach(PropertyInfo pi in pis) { pi.SetValue(nc,pi.Name,dt.rows[0][pi.Name]) }
相关链接:
特性参考链接
C# 特性(Attribute)C# 特性(Attribute)详细介绍
C#特性详解
反射机制参考链接
详解C#中的反射C# 反射(Reflection)
相关文章推荐
- Java反射随记
- c#调用COM组件
- AS3中的反射(速记) 分析
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法