当前位置:K88软件开发文章中心编程语言.NET.NET02 → 文章内容

C# 特性(Attribute)

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-14 1:48:26

er; 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{ // 成员变量 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()); }}在下一章中,我们将使用 Reflection 类对象来检索这些信息。

上一页  [1] [2] 


C# 特性(Attribute)