属性(Attributes)是一个声明性标签,用于向运行时传递有关程序中各种元素行为的信息,声明性标签由放置在它所用于的元素上方的方括号([])描述。
指定属性的语法如下所示:-
[attribute(positional_parameters, name_parameter=value, ...)] element
NET框架提供了三个预定义的属性-
预定义属性AttributeUsage描述了如何使用自定义属性类,它指定可以应用属性的元素类型。
[AttributeUsage ( validon, AllowMultiple=allowmultiple, Inherited=inherited )]
例如,
[AttributeUsage( AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
此预定义属性标签其执行依赖于指定预处理标识符的条件方法,它将导致方法调用的条件编译,具体取决于指定的值,例如Debug或Trace。例如,它在调试代码时显示变量的值。
指定此属性的语法如下所示:-
[Conditional( conditionalSymbol )]
例如,
[Conditional("DEBUG")]
以下示例演示属性-
#define DEBUG using System; using System.Diagnostics; public class Myclass { [Conditional("DEBUG")] public static void Message(string msg) { Console.WriteLine(msg); } } class Test { static void function1() { Myclass.Message("In Function 1."); function2(); } static void function2() { Myclass.Message("In Function 2."); } public static void Main() { Myclass.Message("In Main function."); function1(); Console.ReadKey(); } }
编译并执行上述代码时,将生成以下输出-
In Main function In Function 1 In Function 2
此预定义的属性标签了不应使用的程序实体,它使您可以通知编译器放弃特定的目标元素。
指定此属性的语法如下所示:-
[Obsolete ( message )] [Obsolete ( message, iserror )]
以下程序演示了此-
using System; public class MyClass { [Obsolete("Don't use OldMethod, use NewMethod instead", true)] static void OldMethod() { Console.WriteLine("It is the old method"); } static void NewMethod() { Console.WriteLine("It is the new method"); } public static void Main() { OldMethod(); } }
当您尝试编译程序时,编译器会给出一条错误消息,指出-
Don't use OldMethod, use NewMethod instead
新的自定义属性应该从System.Attribute类派生。例如,
//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
在前面的代码中,无涯教程声明了一个名为DeBugInfo的自定义属性。
让无涯教程构造一个名为DeBugInfo的自定义属性,它存储通过调试任何程序获得的信息。
每个属性必须至少有一个构造函数,位置参数应该通过构造函数传递,以下代码显示了DeBugInfo类-
链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-attributes.html
来源:LearnFk无涯教程网
//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()); } }
在下一章中,无涯教程将使用反射类对象检索属性信息。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)