属性提供功能强大的方法以将声明信息与 C# 代码(类型、方法、属性等)相关联。与程序实体关联后,属性可在运行时查询,并可以以任意多种方式使用。

属性的用法示例包括:

  • 将帮助文档与程序实体关联(通过 Help 属性)。
  • 将值编辑器关联到 GUI 框架中的特定类型(通过 ValueEditor 属性)。

除一个完整的示例外,本教程还包括以下主题:

  • 声明属性类

    第一件需要做的事情是声明属性。

  • 使用属性类

    创建属性后,随即将属性与特定的程序元素关联。

  • 通过反射访问属性

    属性与程序元素关联后,即可使用反射查询属性存在及其值。

声明属性类

在 C# 中声明属性很简单:它采取从 System.Attribute 继承的类声明的形式,并已用 AttributeUsage 属性标记,如下所示:

 1在c#中使用属性(Attribute)--转载自100down.comusing System;
 2在c#中使用属性(Attribute)--转载自100down.com[AttributeUsage(AttributeTargets.All)]
 3在c#中使用属性(Attribute)--转载自100down.compublic class HelpAttribute : System.Attribute 
 4}

代码讨论

  • 属性 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。

使用属性类

以下是使用上一节中声明的属性的简单示例:

1在c#中使用属性(Attribute)--转载自100down.com[HelpAttribute("http://localhost/MyClassInfo")]
2在c#中使用属性(Attribute)--转载自100down.comclass MyClass 
3}

本示例中,HelpAttribute 属性与 MyClass 关联。

注意   根据约定,所有属性名称都以单词“Attribute”结束,以便将它们与 .NET Framework 中的其他项区分。但是,在代码中使用属性时不需要指定属性后缀。例如,可以如下指定 HelpAttribute:

1在c#中使用属性(Attribute)--转载自100down.com[Help("http://localhost/MyClassInfo")] // [Help] == [HelpAttribute]
2在c#中使用属性(Attribute)--转载自100down.comclass MyClass 
3}

通过反射访问属性

属性与程序元素关联后,可以使用反射查询属性存在及其值。查询属性的主要反射方法包含在 System.Reflection.MemberInfo 类(GetCustomAttributes 方法族)中。下面的示例演示使用反射获取对属性的访问的基本方法:

 1在c#中使用属性(Attribute)--转载自100down.comclass MainClass 
 2 

示例

下面是集合所有部分的完整示例。

 

  1在c#中使用属性(Attribute)--转载自100down.com// AttributesTutorial.cs
  2在c#中使用属性(Attribute)--转载自100down.com// This example shows the use of class and method attributes.
  3在c#中使用属性(Attribute)--转载自100down.com
  4在c#中使用属性(Attribute)--转载自100down.comusing System;
  5在c#中使用属性(Attribute)--转载自100down.comusing System.Reflection;
  6在c#中使用属性(Attribute)--转载自100down.comusing System.Collections;
  7在c#中使用属性(Attribute)--转载自100down.com
  8在c#中使用属性(Attribute)--转载自100down.com// The IsTested class is a user-defined custom attribute class.
  9在c#中使用属性(Attribute)--转载自100down.com// It can be applied to any declaration including
 10在c#中使用属性(Attribute)--转载自100down.com//  - types (struct, class, enum, delegate)
 11在c#中使用属性(Attribute)--转载自100down.com//  - members (methods, fields, events, properties, indexers)
 12在c#中使用属性(Attribute)--转载自100down.com// It is used with no arguments.
 13在c#中使用属性(Attribute)--转载自100down.compublic class IsTestedAttribute : Attribute
 14}

输出

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!

相关文章: