C#特性与反射机制

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])
}

相关链接:

特性参考链接
反射机制参考链接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值