1class MainClass 2{ 3publicstaticvoid Main() 4{ 5 System.Reflection.MemberInfo info =typeof(MyClass); 6object[] attributes = info.GetCustomAttributes(true); 7for (int i =0; i < attributes.Length; i ++) 8{ 9 System.Console.WriteLine(attributes[i]); 10 } 11 } 12}
示例
下面是集合所有部分的完整示例。
1// AttributesTutorial.cs 2// This example shows the use of class and method attributes. 3 4using System; 5using System.Reflection; 6using System.Collections; 7 8// The IsTested class is a user-defined custom attribute class. 9// It can be applied to any declaration including 10// - types (struct, class, enum, delegate) 11// - members (methods, fields, events, properties, indexers) 12// It is used with no arguments. 13publicclass IsTestedAttribute : Attribute 14{ 15publicoverridestring ToString() 16{ 17return"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)] 26publicclass AuthorAttribute : Attribute 27{ 28// This constructor specifies the unnamed arguments to the attribute class. 29public AuthorAttribute(string name) 30{ 31this.name = name; 32this.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. 37publicstring Name 38{ 39get 40{ 41return 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 this 47// class as an attribute class. 48publicint Version 49{ 50get 51{ 52return version; 53 } 54set 55{ 56 version = value; 57 } 58 } 59 60publicoverridestring ToString() 61{ 62string value ="Author : "+ Name; 63if (version !=0) 64{ 65 value +=" Version : "+ Version.ToString(); 66 } 67return value; 68 } 69 70privatestring name; 71privateint 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")] 78class Account 79{ 80// Attach the IsTestedAttribute custom attribute to this method. 81 [IsTested] 82publicvoid AddOrder(Order orderToAdd) 83{ 84 orders.Add(orderToAdd); 85 } 86 87private 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()] 94class Order 95{ 96// add stuff here 97} 98 99class MainClass 100{ 101privatestaticbool IsMemberTested(MemberInfo member) 102{ 103foreach (object attribute in member.GetCustomAttributes(true)) 104{ 105if (attribute is IsTestedAttribute) 106{ 107returntrue; 108 } 109 } 110returnfalse; 111 } 112 113privatestaticvoid DumpAttributes(MemberInfo member) 114{ 115 Console.WriteLine("Attributes for : "+ member.Name); 116foreach (object attribute in member.GetCustomAttributes(true)) 117{ 118 Console.WriteLine(attribute); 119 } 120 } 121 122publicstaticvoid Main() 123{ 124// display attributes for Account class 125 DumpAttributes(typeof(Account)); 126 127// display list of tested members 128foreach (MethodInfo method in (typeof(Account)).GetMethods()) 129{ 130if (IsMemberTested(method)) 131{ 132 Console.WriteLine("Member {0} is tested!", method.Name); 133 } 134else 135{ 136 Console.WriteLine("Member {0} is NOT tested!", method.Name); 137 } 138 } 139 Console.WriteLine(); 140 141// display attributes for Order class 142 DumpAttributes(typeof(Order)); 143 144// display attributes for methods on the Order class 145foreach (MethodInfo method in (typeof(Order)).GetMethods()) 146{ 147if (IsMemberTested(method)) 148{ 149 Console.WriteLine("Member {0} is tested!", method.Name); 150 } 151else 152{ 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!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端