using System;using System.Reflection;namespace BugFixApplication {//a custom attribute BugFix to be//assigned to a class and its members
编程学习网为您整理以下代码实例,主要实现:C#反射DeBugInfo,希望可以帮到各位朋友。
using System;
using System.Reflection;
namespace BUGFixApplication {
//a custom attribute BUGFix to be
//assigned to a class and its members
[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.FIEld |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]
public class DeBUGInfo : System.Attribute {
private int BUGNo;
private string developer;
private string lastRevIEw;
public string message;
public DeBUGInfo(int bg, string dev, string d) {
this.BUGNo = bg;
this.developer = dev;
this.lastRevIEw = d;
}
public int BUGNo {
get {
return BUGNo;
}
}
public string Developer {
get {
return developer;
}
}
public string LastRevIEw {
get {
return lastRevIEw;
}
}
public string Message {
get {
return message;
}
set {
message = value;
}
}
}
[DeBUGInfo(45, "Zara Ali", "12/8/2012", Message = "Return type mismatch")]
[DeBUGInfo(49, "Nuha Ali", "10/10/2012", Message = "Unused variable")]
class Rectangle {
//member variables
protected double length;
protected double wIDth;
public Rectangle(double l, double w) {
length = l;
wIDth = w;
}
[DeBUGInfo(55, "Zara Ali", "19/10/2012", Message = "Return type mismatch")]
public double GetArea() {
return length * wIDth;
}
[DeBUGInfo(56, "Zara Ali", "19/10/2012")]
public voID display() {
Console.Writeline("Length: {0}", length);
Console.Writeline("WIDth: {0}", wIDth);
Console.Writeline("Area: {0}", GetArea());
}
}//end class Rectangle
class ExecuteRectangle {
static voID Main(string[] args) {
Rectangle r = new Rectangle(4.5, 7.5);
r.display();
Type type = typeof(Rectangle);
//iterating through the attribtues of the Rectangle class
foreach (Object attributes in type.GetCustomAttributes(false)) {
DeBUGInfo dbi = (DeBUGInfo)attributes;
if (null != dbi) {
Console.Writeline("BUG no: {0}", dbi.BUGNo);
Console.Writeline("Developer: {0}", dbi.Developer);
Console.Writeline("Last RevIEwed: {0}", dbi.LastRevIEw);
Console.Writeline("Remarks: {0}", dbi.Message);
}
}
//iterating through the method attribtues
foreach (MethodInfo m in type.getmethods()) {
foreach (Attribute a in m.GetCustomAttributes(true)) {
DeBUGInfo dbi = (DeBUGInfo)a;
if (null != dbi) {
Console.Writeline("BUG no: {0}, for Method: {1}", dbi.BUGNo, m.name);
Console.Writeline("Developer: {0}", dbi.Developer);
Console.Writeline("Last RevIEwed: {0}", dbi.LastRevIEw);
Console.Writeline("Remarks: {0}", dbi.Message);
}
}
}
Console.Readline();
}
}
}
沃梦达教程
本文标题为:C#反射DeBugInfo
基础教程推荐
猜你喜欢
- C#带参数的构造函数 1970-01-01
- C#检查两个矩阵是否相同 1970-01-01
- C#检查当前线程的状态 1970-01-01
- C# do...while循环 1970-01-01
- C#冒泡排序程序 1970-01-01
- 在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型 2022-10-19
- C# ArrayList类 1970-01-01
- C#封装public访问修饰符 1970-01-01
- C#运算符优先级 1970-01-01
- C#按值传递参数 1970-01-01