属性是一个声明性的标签,用于向运行时传递有关程序中各种元素(如类,方法,结构,枚举器,程序集等)的行为的信息。可以通过使用属性将声明性信息添加到程序。声明式标签由放置在其所用元素上方的方括号([]
)表示。
属性用于向程序添加元数据,如编译器指令和其他信息,如注释,描述,方法和类。.net框架提供了两种类型的属性:预定义属性和自定义构建的属性。
用于指定属性的语法如下:
[attribute(positional_parameters, name_parameter = value, ...)]
element
属性名称及其值在方括号内,在应用该属性的元素之前指定。位置参数指定必要信息,名称参数指定可选信息。
.net
框架提供了三个预定义的属性:
预定义属性attributeusage
描述了如何使用自定义属性类。它指定可以应用该属性的项目的类型。
用于指定此属性的语法如下:
[attributeusage(
validon,
allowmultiple=allowmultiple,
inherited=inherited
)]
其中,
validon
指定可以放置属性的语言元素。它是枚举器attributetargets
的值的组合。默认值为attributetargets.all
。allowmultiple
(可选)为此属性的allowmultiple
属性提供了一个布尔值。 如果些值为:true
,则表示属性是多次使用。默认值为false
,表示一次性使用。inherited
(可选)为此属性的inherited
属性提供了一个布尔值。 如果此参数值为:true
,则属性由派生类继承。 它默认值为false
(不继承)。例如,
[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
)]
其中,
message
是一个字符串,描述项目过时的原因以及使用的替代方法。iserror
,是一个布尔值。 如果值为true
,则编译器应将该项目的使用视为错误。默认值为false
,编译器生成警告。示例程序如下:
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
.net框架允许创建可用于存储声明性信息的自定义属性,并可在运行时检索。该信息可以根据设计标准和应用需要与任何目标元素相关。
创建和使用自定义属性涉及四个步骤:
最后一步是编写一个简单的程序来读取元数据以找到各种标记。元数据是用于描述其他数据的数据或信息。程序可在运行时访问属性的反射。这将在下一章讨论。
应该从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
类有三个私有属性用于存储前三个信息和一个用于存储消息的公共属性。 因此,错误编号,开发人员名称和审查日期是debuginfo
类的位置参数,并且消息是可选的或命名的参数。
每个属性必须至少有一个构造函数。位置参数应通过构造函数传递。以下代码显示debuginfo
类:
//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, "maxsu", "12/8/2018", message = "return type mismatch")]
[debuginfo(49, "sukyda", "10/10/2018", 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, "maxsu", "19/10/2018", message = "return type mismatch")]
public double getarea()
{
return length * width;
}
[debuginfo(56, "maxsu", "19/10/2018")]
public void display()
{
console.writeline("length: {0}", length);
console.writeline("width: {0}", width);
console.writeline("area: {0}", getarea());
}
}
在下一章中,使用reflection
类对象检索属性信息。