[attribute(positional_parameters, name_parameter = value, ...)]
element
[ ]
中用来定义特性的名称和值,positional_parameters 用来指定基本信息,name_parameter 用来指定可选信息。
[AttributeUsage (
validon,
AllowMultiple = allowmultiple,
Inherited = inherited
)]
[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]
[Conditional(
conditionalSymbol
)]
[Conditional("DEBUG")]
【示例】下面通过示例演示预定义特性 Conditional 的使用。#define DEBUG using System; using System.Diagnostics; namespace task.lmcjl.com { class Demo { static void function1() { Myclass.Message("Function1 函数"); function2(); } static void function2() { Myclass.Message("Function2 函数"); } static void Main(string[] args) { Myclass.Message("Main 函数"); function1(); Console.ReadKey(); } } public class Myclass { [Conditional("DEBUG")] public static void Message(string msg) { Console.WriteLine(msg); } } }运行结果如下:
Main 函数
Function1 函数
Function2 函数
[Obsolete (
message
)]
[Obsolete (
message,
iserror
)]
using System; namespace task.lmcjl.com { class Demo { [Obsolete("OldMethod 已弃用,请改用 NewMethod", true)] static void OldMethod() { Console.WriteLine("已弃用的函数"); } static void NewMethod() { Console.WriteLine("新定义的函数"); } static void Main(string[] args) { OldMethod(); } } }运行结果如下:
demo.cs(18,10): error CS0619: “task.lmcjl.com.Demo.OldMethod()”已过时:“OldMethod 已弃用,请改用 NewMethod”
// 一个自定义特性 BugFix 被赋给类及其成员
[AttributeUsage(
AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]
public class DeBugInfo : System.Attribute
// 一个自定义特性 BugFix 被赋给类及其成员 [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; } } }
using System; namespace task.lmcjl.com { class Demo { static void Main(string[] args) { Rectangle rec = new Rectangle(12, 15); rec.Display(); } } [DeBugInfo(45, "Zara Ali", "12/8/2012", Message = "返回值类型不匹配")] [DeBugInfo(49, "Nuha Ali", "10/10/2012", Message = "未使用变量")] 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 = "返回值类型不匹配")] 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()); } } // 一个自定义特性 BugFix 被赋给类及其成员 [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; } } } }运行结果如下:
Length: 12
Width: 15
Area: 180
本文链接:http://task.lmcjl.com/news/18320.html