ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Attibutes(特性)


1.A,示例(Sample 


“特性”示例


本示例演示了如何创建自定义特性类,如何在代码中使用这些类,以及如何通过反射查询它们。有关特性的其他信息,请参见特性(C# 编程指南).



提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。对于因将此代码示例用于其他用途而出现的偶然或必然的损害,Microsoft 不承担任何责任。



在 Visual Studio 中生成并运行“特性”示例



  • 在“调试”菜单上,单击“开始执行(不调试)”。


从命令行生成并运行“特性”示例



  • 在命令提示符处,键入以下命令:

csc AttributesTutorial.cs
AttributesTutorial





1.B,示例代码(Sample Code) ​

1.B.1, AttributesTutorial.cs

ylbtech-LanguageSamples-Attibutes(特性)_特性ylbtech-LanguageSamples-Attibutes(特性)_自定义_02

 此代码的发布遵从 // Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。 // //版权所有(C) Microsoft Corporation。保留所有权利。  // AttributesTutorial.cs // 本示例表明类特性和方法特性的用法。  using System; using System.Reflection; using System.Collections;  // IsTested 类是用户定义的自定义特性类。 // 它可以应用于任何声明,包括 //  - 类型(结构、类、枚举、委托) //  - 成员(方法、字段、事、属性、索引器) // 使用它时不带参数。 public class IsTestedAttribute : Attribute {     public override string ToString()     {         return "Is Tested";     } }  // AuthorAttribute 类是用户定义的特性类。 // 它只能应用于类和结构声明。 // 它采用一个未命名的字符串参数(作者的姓名)。 // 它有一个可选的命名参数 Version,其类型为 int。 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class AuthorAttribute : Attribute {     // 此构造函数为特性类指定未命名的参数。     public AuthorAttribute(string name)     {         this.name = name;         this.version = 0;     }      // 此属性为只读(它没有 set 访问器)     // 因此,不能将其用作此特性的命名参数。     public string Name      {         get          {             return name;         }     }      // 此属性可读写(它有 set 访问器)     // 因此,将此类用作属性类时,     // 可以将其用作命名参数。     public int Version     {         get          {             return version;         }         set          {             version = value;         }     }      public override string ToString()     {         string value = "Author : " + Name;         if (version != 0)         {             value += " Version : " + Version.ToString();         }         return value;     }      private string name;     private int version; }  // 此处,将用户定义的自定义特性 AuthorAttribute 附加 // 到 Account 类。创建属性时,会将未命名的 // 字符串参数传递到 AuthorAttribute 类的构造函数。 [Author("Joe Programmer")] class Account {     // 将自定义特性 IsTestedAttribute 附加到此方法。     [IsTested]     public void AddOrder(Order orderToAdd)     {         orders.Add(orderToAdd);     }      private ArrayList orders = new ArrayList(); }  // 将自定义特性 AuthorAttribute 和 IsTestedAttribute 附加 // 到此类。 // 请注意 AuthorAttribute 的命名参数“Version”的用法。 [Author("Jane Programmer", Version = 2), IsTested()] class Order {     // 在此处添加资料... }  class MainClass {    private static bool IsMemberTested(MemberInfo member)    {         foreach (object attribute in member.GetCustomAttributes(true))         {             if (attribute is IsTestedAttribute)             {                return true;             }         }       return false;    }      private static void DumpAttributes(MemberInfo member)     {         Console.WriteLine("Attributes for : " + member.Name);         foreach (object attribute in member.GetCustomAttributes(true))         {             Console.WriteLine(attribute);         }     }      public static void Main()     {         // 显示 Account 类的特性         DumpAttributes(typeof(Account));          // 显示已测试成员的列表         foreach (MethodInfo method in (typeof(Account)).GetMethods())         {             if (IsMemberTested(method))             {                Console.WriteLine("Member {0} is tested!", method.Name);             }             else             {                Console.WriteLine("Member {0} is NOT tested!", method.Name);             }         }         Console.WriteLine();          // 显示 Order 类的特性         DumpAttributes(typeof(Order));          // 显示 Order 类的方法的特性         foreach (MethodInfo method in (typeof(Order)).GetMethods())         {            if (IsMemberTested(method))            {                Console.WriteLine("Member {0} is tested!", method.Name);            }            else            {                Console.WriteLine("Member {0} is NOT tested!", method.Name);            }         }         Console.WriteLine();     } } 

View Code

1.B.2,

1.C,下载地址(Free Download