在c#中使用属性(Attribute)--转载自100down.com
属性提供功能强大的方法以将声明信息与 C# 代码(类型、方法、属性等)相关联。与程序实体关联后,属性可在运行时查询,并可以以任意多种方式使用。
属性的用法示例包括:
- 将帮助文档与程序实体关联(通过 Help 属性)。
- 将值编辑器关联到 GUI 框架中的特定类型(通过 ValueEditor 属性)。
除一个完整的示例外,本教程还包括以下主题:
- 声明属性类
第一件需要做的事情是声明属性。
- 使用属性类
创建属性后,随即将属性与特定的程序元素关联。
- 通过反射访问属性
属性与程序元素关联后,即可使用反射查询属性存在及其值。
声明属性类
在 C# 中声明属性很简单:它采取从 System.Attribute 继承的类声明的形式,并已用 AttributeUsage 属性标记,如下所示:
using System;2
[AttributeUsage(AttributeTargets.All)]3
public class HelpAttribute : System.Attribute 4
{5
public readonly string Url;6

7
public string Topic // Topic is a named parameter8
{9
get 10
{ 11
return topic; 12
}13
set 14
{ 16
topic = value; 17
}18
}19

20
public HelpAttribute(string url) // url is a positional parameter21
{22
this.Url = url;23
}24

25
private string topic;26
}代码讨论
- 属性 AttributeUsage 指定该属性可以应用于的语言元素。
- 属性类是从 System.Attribute 派生的公共类,至少有一个公共构造函数。
- 属性类有两种类型的参数:
- “定位参数”,每次使用属性时都必须指定这些参数。定位参数被指定为属性类的构造函数参数。在上面的示例中,url 便是一个定位参数。
- “命名参数”,可选。如果使用属性时指定了命名参数,则必须使用参数的名称。通过包含非静态字段或属性来定义命名参数。在上面的示例中,Topic 便是一个命名参数。
- 属性参数限制为下列类型的常数值:
- 简单类型(bool、byte、char、short、int、long、float 和 double)
- string
- System.Type
- enums
- 对象(对象类型的属性参数的参数必须是属于上述类型之一的常数值。)
- 以上任意类型的一维数组
AttributeUsage 属性的参数
属性 AttributeUsage 提供声明属性的基础机制。
AttributeUsage 具有一个定位参数:
- AllowOn 指定可以将属性赋给的程序元素(类、方法、属性、参数等)。该参数的有效值可以在 .NET Framework 中的 System.Attributes.AttributeTargets 枚举中找到。该参数的默认值是所有程序元素 (AttributeElements.All)。
AttributeUsage 有一个命名参数:
- AllowMultiple,一个布尔值,指示是否可以为一个程序元素指定多个属性。该参数的默认值为 false。
使用属性类
以下是使用上一节中声明的属性的简单示例:
[HelpAttribute("http://localhost/MyClassInfo")]2
class MyClass 3
{4
}本示例中,HelpAttribute 属性与 MyClass 关联。
注意 根据约定,所有属性名称都以单词“Attribute”结束,以便将它们与 .NET Framework 中的其他项区分。但是,在代码中使用属性时不需要指定属性后缀。例如,可以如下指定 HelpAttribute:
1
[Help("http://localhost/MyClassInfo")] // [Help] == [HelpAttribute]
2
class MyClass
3
{
4
}
[Help("http://localhost/MyClassInfo")] // [Help] == [HelpAttribute]2
class MyClass 3
{4
}通过反射访问属性
属性与程序元素关联后,可以使用反射查询属性存在及其值。查询属性的主要反射方法包含在 System.Reflection.MemberInfo 类(GetCustomAttributes 方法族)中。下面的示例演示使用反射获取对属性的访问的基本方法:
class MainClass 2
{3
public static void Main() 4
{5
System.Reflection.MemberInfo info = typeof(MyClass);6
object[] attributes = info.GetCustomAttributes(true);7
for (int i = 0; i < attributes.Length; i ++)8
{9
System.Console.WriteLine(attributes[i]);10
}11
} 12
} 示例
下面是集合所有部分的完整示例。
// AttributesTutorial.cs2
// This example shows the use of class and method attributes.3

4
using System;5
using System.Reflection;6
using System.Collections;7

8
// The IsTested class is a user-defined custom attribute class.9
// It can be applied to any declaration including10
// - types (struct, class, enum, delegate)11
// - members (methods, fields, events, properties, indexers)12
// It is used with no arguments.13
public class IsTestedAttribute : Attribute14
{15
public override string ToString()16
{17
return "Is Tested";18
}19
}20

21
// The AuthorAttribute class is a user-defined attribute class.22
// It can be applied to classes and struct declarations only.23
// It takes one unnamed string argument (the author's name).24
// It has one optional named argument Version, which is of type int.25
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]26
public class AuthorAttribute : Attribute27
{28
// This constructor specifies the unnamed arguments to the attribute class.29
public AuthorAttribute(string name)30
{31
this.name = name;32
this.version = 0;33
}34

35
// This property is readonly (it has no set accessor)36
// so it cannot be used as a named argument to this attribute.37
public string Name 38
{39
get 40
{41
return name;42
}43
}44

45
// This property is read-write (it has a set accessor)46
// so it can be used as a named argument when using this47
// class as an attribute class.48
public int Version49
{50
get 51
{52
return version;53
}54
set 55
{56
version = value;57
}58
}59

60
public override string ToString()61
{62
string value = "Author : " + Name;63
if (version != 0)64
{65
value += " Version : " + Version.ToString();66
}67
return value;68
}69

70
private string name;71
private int version;72
}73

74
// Here you attach the AuthorAttribute user-defined custom attribute to 75
// the Account class. The unnamed string argument is passed to the 76
// AuthorAttribute class's constructor when creating the attributes.77
[Author("Joe Programmer")]78
class Account79
{80
// Attach the IsTestedAttribute custom attribute to this method.81
[IsTested]82
public void AddOrder(Order orderToAdd)83
{84
orders.Add(orderToAdd);85
}86

87
private ArrayList orders = new ArrayList();88
}89

90
// Attach the AuthorAttribute and IsTestedAttribute custom attributes 91
// to this class.92
// Note the use of the 'Version' named argument to the AuthorAttribute.93
[Author("Jane Programmer", Version = 2), IsTested()]94
class Order95
{96
// add stuff here 
97
}98

99
class MainClass100
{101
private static bool IsMemberTested(MemberInfo member)102
{103
foreach (object attribute in member.GetCustomAttributes(true))104
{105
if (attribute is IsTestedAttribute)106
{107
return true;108
}109
}110
return false;111
}112

113
private static void DumpAttributes(MemberInfo member)114
{115
Console.WriteLine("Attributes for : " + member.Name);116
foreach (object attribute in member.GetCustomAttributes(true))117
{118
Console.WriteLine(attribute);119
}120
}121

122
public static void Main()123
{124
// display attributes for Account class125
DumpAttributes(typeof(Account));126

127
// display list of tested members128
foreach (MethodInfo method in (typeof(Account)).GetMethods())129
{130
if (IsMemberTested(method))131
{132
Console.WriteLine("Member {0} is tested!", method.Name);133
}134
else135
{136
Console.WriteLine("Member {0} is NOT tested!", method.Name);137
}138
}139
Console.WriteLine();140

141
// display attributes for Order class142
DumpAttributes(typeof(Order));143

144
// display attributes for methods on the Order class145
foreach (MethodInfo method in (typeof(Order)).GetMethods())146
{147
if (IsMemberTested(method))148
{149
Console.WriteLine("Member {0} is tested!", method.Name);150
}151
else152
{153
Console.WriteLine("Member {0} is NOT tested!", method.Name);154
}155
}156
Console.WriteLine();157
}158
}输出
Attributes for : Account
Author : Joe Programmer
Member GetHashCode is NOT tested!
Member Equals is NOT tested!
Member ToString is NOT tested!
Member AddOrder is tested!
Member GetType is NOT tested!
Attributes for : Order
Author : Jane Programmer Version : 2
Is Tested
Member GetHashCode is NOT tested!
Member Equals is NOT tested!
Member ToString is NOT tested!
Member GetType is NOT tested!



浙公网安备 33010602011771号