从上篇里我们可以看到,Attribute似乎总跟public、static这些关键字(Keyword)出现在一起。莫非使用了Attribute就相当于定义了新的修饰符(Modifier)吗?让我们来一窥究竟!
#define
OK
using System;
using System.Diagnostics;
namespace Sample
{
class @H_301_43@Program
{
[Conditional("OK")]
public static void TargetMethod()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("/t=< @H_301_43@水之真谛>=/nhttp://blog.csdn.net/FantasiaX/n/n" );
}
static void Main(string[] args)
{
TargetMethod();
}
}
}
using System;
using System.Diagnostics;
namespace Sample
{
class @H_301_43@Program
{
[Conditional("OK")]
public static void TargetMethod()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("/t=< @H_301_43@水之真谛>=/nhttp://blog.csdn.net/FantasiaX/n/n" );
}
static void Main(string[] args)
{
TargetMethod();
}
}
}
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@ 接下来,让我们把编译出的结果(.EXE文件)用“微软中间语言反编译器”打开,查看存储在程序集(Assembly,这在个例子中就是这个.EXE文件)中的中间语言代码(中间语言也就是我们常说的通用语言)。
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@ 严格地来说,用来形成上图中树状结构的代码并不是程序集中的中间语言,而主要是元数据(Metadata)的功劳。包含在程序集中的元数据记录了这个程序集里有多少个namespace、多少个类、类里有什么成员、成员的访问级别是什么……而且,元数据是以文本(也就是Unicode字符)形式存在的,使用.NET的反射(Reflection)技术,很容易就能把它们读取出来并形成各种各样的漂亮视图——上面的树状图、VS里的Object Browser视图和自动代码提示功能,都是元数据与反射技术结合的产物。一个程序集(.EXE或.DLL)能够使用包含在自己体内的元数据来完整地说明自己,而不必像C/C++那样带着一大捆头文件,这就叫作“自包含性”或“自描述性”。
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@ 扯的有点儿远了——让我们回到正题,双击反编译器中的TargetMethod:void()。这回弹出窗口里显示的内容是真正的微软中间语言代码了。这些代码也都是文本形式的,需要经过.NET的“虚拟机”再编译后才能被cpu所执行。顺便说一句:VB.NET代码也会编译成这样的中间代码,所以,.NET平台上所有语言的编译结果都是通用的。换句话说,你用C#编写了一个组件,把它编译成一个DLL文件并交给VB.NET程序员,VB.NET程序员可以直接使用,丝毫不必有任何担心J
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@ 仔细观察中间代码之后,Attribute变得了无秘密!图中蓝色箭头所指处是两个“真正的”修饰符——Attribute并没有出现在这里。而在红色箭头所标识的位置,我们可以清楚地看出——这分明是在调用mscorlib.dll程序集System.Diagnostics名称空间中ConditionalAttribute类的构造函数。可见,Attribute并不是修饰符,而是一个有着独特实例化形式的类!
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@God,我怀疑是不是讲的太深了。没关系,上面关于中间语言的东西你都可以不care,只需要记住一个结论就可以了——我们已经从“底层”证明了Attribute不是什么“修饰符”,而是一种实例化方式比较特殊的类。
Attribute的实例化
@H_301_43@
2.
@H_301_43@方括号必需紧挨着放置在被附着目标的前面。
2.
@H_301_43@构造函数参数的顺序不能错,这个很容易理解——调用任何一个函数你都不能改变参数的顺序——除非它有相应的重载(Overload)。因为这个顺序的固定的,所以有些书里管这些参数称为“定位参数”,意即“个数和位置固定的参数”。
3.
@H_301_43@对Attribute实例的属性的赋值可有可无——反正它会有一个默认值。而且,先对哪个属性赋值、后对哪个属性赋值不受限制。有些书管这些为属性赋值的参数叫“具名参数”——令人匪夷所思。
自己动手写Attribute
@H_301_43@
@H_301_43@//======水之真谛
@H_301_43@=======//
// 上善若水,润物无声 @H_301_43@//
/* http://blog.csdn.net/FantasiaX */
using @H_301_43@ System;
namespace OysterAttributeSample
{
class Oyster: System.Attribute // 必需以System.Attribute类为基类
@H_301_43@ {
// Kind属性,默认值为 null
@H_301_43@ private string kind;
public string Kind
{
get { return kind; }
set { kind = value; }
}
// Age属性,默认值为
@H_301_43@ private uint age;
public uint Age
{
get { return age; }
set { age = value; }
}
// 值为null的string是危险的,所以必需在构造函数中赋值
@H_301_43@ public Oyster(string arg) // 定位参数
@H_301_43@ {
this.Kind = arg;
}
}
[Oyster("Thorny ",Age=3)] // 3年的多刺牡蛎附着在轮船(这是一个类)上。注意:对属性的赋值是在圆括号里完成的!
@H_301_43@ class @H_301_43@Ship
{
[Oyster("Saddle")] // 0 @H_301_43@年的鞍形牡蛎附着在船舵(这是一个数据成员)上,Age使用的是默认值,构造函数的参数必需完整
@H_301_43@ public string Rudder;
}
class @H_301_43@Program
{
static void Main(string[] args)
{
// ... @H_301_43@使用反射来读取 Attribute
@H_301_43@ }
}
}
// 上善若水,润物无声 @H_301_43@//
/* http://blog.csdn.net/FantasiaX */
using @H_301_43@ System;
namespace OysterAttributeSample
{
class Oyster: System.Attribute // 必需以System.Attribute类为基类
@H_301_43@ {
// Kind属性,默认值为 null
@H_301_43@ private string kind;
public string Kind
{
get { return kind; }
set { kind = value; }
}
// Age属性,默认值为
@H_301_43@ private uint age;
public uint Age
{
get { return age; }
set { age = value; }
}
// 值为null的string是危险的,所以必需在构造函数中赋值
@H_301_43@ public Oyster(string arg) // 定位参数
@H_301_43@ {
this.Kind = arg;
}
}
[Oyster("Thorny ",Age=3)] // 3年的多刺牡蛎附着在轮船(这是一个类)上。注意:对属性的赋值是在圆括号里完成的!
@H_301_43@ class @H_301_43@Ship
{
[Oyster("Saddle")] // 0 @H_301_43@年的鞍形牡蛎附着在船舵(这是一个数据成员)上,Age使用的是默认值,构造函数的参数必需完整
@H_301_43@ public string Rudder;
}
class @H_301_43@Program
{
static void Main(string[] args)
{
// ... @H_301_43@使用反射来读取 Attribute
@H_301_43@ }
}
}
@H_301_43@为了不把代码拖的太长,上面这个例子中Oyster类的构造函数只有一个参数,所以对“定位参数”体现的还不够淋漓尽致。大家可以再为Oyster类添加几个属性,并在构造函数里多设置几个参数,体验一下Attribute实例化时对参数个数及参数位置的敏感性。
能被Attribute所附着的目标
@H_301_43@
@H_301_43@
=============================================================================
All Assembly Class Constructor
Delegate Enum Event Field
GenericParameter Interface Method Module
Parameter Property ReturnValue Struct
=============================================================================
@H_301_43@
@H_301_43@
@H_301_43@// =<水之真谛
@H_301_43@>=
// http://blog.csdn.net/FantasiaX
using System;
namespace AttributeTargetValue
{
class @H_301_43@Program
{
static void Main(string[] args)
{
Console.WriteLine("Assembly/t/t/t{0}",Convert.ToInt32(AttributeTargets.Assembly));
Console.WriteLine("Module/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Module));
Console.WriteLine("Class/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Class));
Console.WriteLine("Struct/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Struct));
Console.WriteLine("Enum/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Enum));
Console.WriteLine("Constructor/t/t/t{0}",Convert.ToInt32(AttributeTargets.Constructor));
Console.WriteLine("Method/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Method));
Console.WriteLine("Property/t/t/t{0}",Convert.ToInt32(AttributeTargets.Property));
Console.WriteLine("Field/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Field));
Console.WriteLine("Event/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Event));
Console.WriteLine("Interface/t/t/t{0}",Convert.ToInt32(AttributeTargets.Interface));
Console.WriteLine("Parameter/t/t/t{0}",Convert.ToInt32(AttributeTargets.Parameter));
Console.WriteLine("Delegate/t/t/t{0}",Convert.ToInt32(AttributeTargets.Delegate));
Console.WriteLine("ReturnValue/t/t/t{0}",Convert.ToInt32(AttributeTargets.ReturnValue));
Console.WriteLine("GenericParameter/t/t{0}",Convert.ToInt32(AttributeTargets.GenericParameter));
Console.WriteLine("All/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.All));
Console.WriteLine("/n");
}
}
}
// http://blog.csdn.net/FantasiaX
using System;
namespace AttributeTargetValue
{
class @H_301_43@Program
{
static void Main(string[] args)
{
Console.WriteLine("Assembly/t/t/t{0}",Convert.ToInt32(AttributeTargets.Assembly));
Console.WriteLine("Module/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Module));
Console.WriteLine("Class/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Class));
Console.WriteLine("Struct/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Struct));
Console.WriteLine("Enum/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Enum));
Console.WriteLine("Constructor/t/t/t{0}",Convert.ToInt32(AttributeTargets.Constructor));
Console.WriteLine("Method/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Method));
Console.WriteLine("Property/t/t/t{0}",Convert.ToInt32(AttributeTargets.Property));
Console.WriteLine("Field/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Field));
Console.WriteLine("Event/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.Event));
Console.WriteLine("Interface/t/t/t{0}",Convert.ToInt32(AttributeTargets.Interface));
Console.WriteLine("Parameter/t/t/t{0}",Convert.ToInt32(AttributeTargets.Parameter));
Console.WriteLine("Delegate/t/t/t{0}",Convert.ToInt32(AttributeTargets.Delegate));
Console.WriteLine("ReturnValue/t/t/t{0}",Convert.ToInt32(AttributeTargets.ReturnValue));
Console.WriteLine("GenericParameter/t/t{0}",Convert.ToInt32(AttributeTargets.GenericParameter));
Console.WriteLine("All/t/t/t/t{0}",Convert.ToInt32(AttributeTargets.All));
Console.WriteLine("/n");
}
}
}
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@ 我想你一定发现了规律——除了All的值之外,每个值的二进制形式中只有一位是“1”,其余位全是“0”。这就是枚举值的另一种用法——标识位。那么,标识位有什么好处呢?
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@AttributeTargets.Class | AttributeTargets.Method
@H_301_43@
@H_301_43@就可以了。因为这两个枚举值的标识位(也就是那个唯一的“1”)是错开的,所以只需要按位求或就解决问题了。我想,聪明的你一定立刻就能解释为什么AttributeTargets.All的值是32767了吧:p
@H_301_43@ 默认情况下,当我们声明并定义一个新Attribute类时,它的可附着目标是AttributeTargets.All。大多数情况下AttributeTargets.All就已经满足需求了,不过,如果你非要对它有所限制,那就要费点儿周折了。
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@ [AttributeUsage(AttributeTargets.Class|AttributeTargets.Field)]
class Oyster : System.Attribute
{
// OysterAttribute类的具体实现
@H_301_43@ }
class Oyster : System.Attribute
{
// OysterAttribute类的具体实现
@H_301_43@ }
@H_301_43@ 没想到吧!原来是用Attribute(的实例)附着在Attribute(类)上!本来吗,Attribute的本质就是类,而AttributeTargets.Class 又说明Attribute可以附着在类上,所以,使用Attribute来“修饰”Attribute也就顺理成章了J
@H_301_43@
@H_301_43@
@H_301_43@
@H_301_43@ [AttributeUsage(AttributeTargets.Class | AttributeTargets.Field,Inherited = false,AllowMultiple = true)]
class Oyster : System.Attribute
{
// OysterAttribute类的具体实现
@H_301_43@ }
class Oyster : System.Attribute
{
// OysterAttribute类的具体实现
@H_301_43@ }
@H_301_43@ 原来,AttributeUsage这个用来专门修饰Attribute的Attribute除了可以控制修饰目标外,还能决定被它修饰的Attribute是否能够随宿主“遗传”以及是否可以使用多个实例来修饰同一个目标!
@H_301_43@
转自:http://liutiemeng.blog.51cto.com/120361/29203