C#Attribute的使用
2017-01-11 00:08
489 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; class Program { static void Main(string[] args) { Rectangle r = new Rectangle(4.5, 7.5); r.Display(); Type type = typeof(Rectangle); foreach( Object attributes in type.GetCustomAttributes(false)) { DebugInfo dbi = (DebugInfo)attributes; if( null != dbi ) { ShowAttributeInfo(dbi); } } foreach( MethodInfo m in type.GetMethods() ) { foreach( Attribute a in m.GetCustomAttributes(true)) { DebugInfo dbi = (DebugInfo)a; if( null != dbi ) { ShowAttributeInfo(dbi); } } } Console.ReadLine(); } private static void ShowAttributeInfo( DebugInfo dbi ) { Console.WriteLine("\n================"); Console.WriteLine("Bug No = {0}", dbi.BugNo); Console.WriteLine("Developer = {0}", dbi.Developer); Console.WriteLine("LastReview = {0}", dbi.LastReview); Console.WriteLine("Msg = {0}", dbi.Message); } } [DebugInfo(1,"YuXiKuo","11/11/2017", Message = "Return type mismatch")] [DebugInfo(2,"WangDongGe","10/10/2017", Message = "Unused variable")] class Rectangle { protected double length; protected double width; public Rectangle( double l, double w ) { length = l; width = w; } [DebugInfo(3,"YuXiKuo","10/5/2017",Message = "GetArea")] public double GetArea() { return length * width; } [DebugInfo(4, "YuXiKuo", "10/5/2017", Message = "Display")] public void Display() { Console.WriteLine("Length = {0}", length); Console.WriteLine("Width = {0}", width); Console.WriteLine("Area = {0}", GetArea()); } }
相关文章推荐
- 使用组件来保护你的ASP代码
- SCI软件使用方法总结-定位测量篇
- IP 伪装简易使用说明
- 源码推荐:一个使用C#绘制图形引擎的Framework
- Win32汇编教程六 工具栏和状态栏的使用
- Win32汇编教程五 菜单和加速键的使用
- Win32汇编教程三 一个简单的对话框 --- 兼谈资源文件的使用
- 使用ActiveX控件开发网页常见的问题
- 使用 ASP+ DataGrid 控件来创建主视图/详细资料视图 (2)
- 使用 ASP+ DataGrid 控件来创建主视图/详细资料视图
- 手册中关于怎样使用TQuery 的误导
- 使用ASP和Word进行服务器端拼写检查
- 单独使用CRecordSet
- 在 Web 页上使用条件数值格式
- 关于PHP与Sybase数据库的连接与使用
- 使用ASP常见问题解答
- [导入]Guru of the Week:#41 使用标准库
- 如何使用ASP进行打印操作
- VB中Winsock控制的UDP协议的使用
- FUNCky 的使用