C#反射访问属性规范及示例

开发 后端
C#具有一个反射系统,可用来检索用自定义属性定义的信息,本文介绍其主要方法。

如果没有检索自定义属性的信息和对其进行操作的方法,则定义自定义属性并将其放置在源代码中就没有意义。C# 具有一个反射系统,可用来检索用自定义属性定义的信息。主要方法是 GetCustomAttributes,它返回对象数组,这些对象在运行时等效于源代码属性。此方法具有多个重载版本。有关更多信息,请参见 Attribute。

C#反射——属性规范

C#

  1. [Author("H. Ackerman", version = 1.1)]  
  2. class SampleClass 

在概念上等效于:

C#

  1. Author anonymousAuthorObject = new Author("H. Ackerman");  
  2. anonymousAuthorObject.version = 1.1; 

但是,直到查询 SampleClass 以获取属性时才会执行此代码。对 SampleClass 调用 GetCustomAttributes 会导致按上述方式构造并初始化一个 Author 对象。如果类还有其他属性,则其他属性对象的以类似方式构造。然后 GetCustomAttributes 返回 Author 对象和数组中的任何其他属性对象。之后就可以对此数组进行迭代,确定根据每个数组元素的类型所应用的属性,并从属性对象中提取信息。

C#反射——示例

下面是一个完整的示例。定义一个自定义属性,将其应用于若干实体并通过反射进行检索。

C#

  1. [System.AttributeUsage(System.AttributeTargets.Class |  
  2.                        System.AttributeTargets.Struct,  
  3.                        AllowMultiple = true)  // multiuse attribute  
  4. ]  
  5. public class Author : System.Attribute  
  6. {  
  7.     string name;  
  8.     public double version;  
  9.  
  10.     public Author(string name)  
  11.     {  
  12.         this.name = name;  
  13.         version = 1.0;  // Default value  
  14.     }  
  15.  
  16.     public string GetName()  
  17.     {  
  18.         return name;  
  19.     }  
  20. }  
  21.  
  22. [Author("H. Ackerman")]  
  23. private class FirstClass  
  24. {  
  25.     // ...  
  26. }  
  27.  
  28. // No Author attribute  
  29. private class SecondClass  
  30. {  
  31.     // ...  
  32. }  
  33.  
  34. [Author("H. Ackerman"), Author("M. Knott", version = 2.0)]  
  35. private class ThirdClass  
  36. {  
  37.     // ...  
  38. }  
  39.  
  40. class TestAuthorAttribute  
  41. {  
  42.     static void Main()  
  43.     {  
  44.         PrintAuthorInfo(typeof(FirstClass));  
  45.         PrintAuthorInfo(typeof(SecondClass));  
  46.         PrintAuthorInfo(typeof(ThirdClass));  
  47.     }  
  48.  
  49.     private static void PrintAuthorInfo(System.Type t)  
  50.     {  
  51.         System.Console.WriteLine("Author information for {0}", t);  
  52.         System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);  // reflection  
  53.  
  54.         foreach (System.Attribute attr in attrs)  
  55.         {  
  56.             if (attr is Author)  
  57.             {  
  58.                 Author a = (Author)attr;  
  59.                 System.Console.WriteLine("   {0}, version {1:f}", a.GetName(), a.version);  
  60.             }  
  61.         }  
  62.     }  

输出

Author information for FirstClass

H. Ackerman, version 1.00

Author information for SecondClass

Author information for ThirdClass

H. Ackerman, version 1.00

M. Knott, version 2.00

本文关于C#反射访问属性的问题就介绍到这里。

【编辑推荐】

  1. C#3.5新特性的介绍
  2. C#中DirectSound录音的使用
  3. C#扩展方法性能测试对比
  4. C#多标签浏览器功能的扩展
  5. C#标签的制作:多标签的实现
责任编辑:book05 来源: hi.baidu
相关推荐

2021-03-15 08:18:23

C#反射模块

2009-08-25 15:50:13

C#连接远程数据库

2009-09-07 03:37:51

C#窗体

2009-08-27 15:53:30

C#中using wo

2009-09-01 17:41:45

C# HelpAttr

2024-03-04 18:49:59

反射C#开发

2009-08-28 13:56:25

C#反射命名空间

2024-04-15 04:00:00

C#反射代码

2009-09-03 11:00:29

C#反射机制

2009-08-21 08:41:44

C#反射

2009-08-27 16:30:08

C#编程命名规范

2009-08-21 08:59:35

C#语言规范

2009-09-01 10:37:51

C#项目代码C#代码规范

2009-08-21 08:52:40

C#语言命名

2009-08-13 13:38:30

C#命名规范

2009-08-21 15:57:58

C# DataSour

2009-08-26 16:46:06

C# ThreadSt

2021-03-18 00:13:27

C#访问器属性

2009-08-28 13:12:56

C#反射实例C#反射

2009-08-12 17:32:44

C#反射方法
点赞
收藏

51CTO技术栈公众号