c# 特性(attribute)
特性(attribute)是用于在运行时传递程序中各种元素(比如类、方法、结构、枚举、组件等)的行为信息的声明性标签。您可以通过使用特性向程序添加声明性信息。一个声明性标签是通过放置在它所应用的元素前面的方括号([ ])来描述的。
特性(attribute)用于添加元数据,如编译器指令和注释、描述、方法、类等其他信息。.net 框架提供了两种类型的特性:预定义特性和自定义特性。
规定特性(attribute)
规定特性(attribute)的语法如下:
[attribute(positional_parameters, name_parameter = value, ...)]
element
特性(attribute)的名称和值是在方括号内规定的,放置在它所应用的元素之前。positional_parameters 规定必需的信息,name_parameter 规定可选的信息。
预定义特性(attribute)
.net 框架提供了三种预定义特性:
attributeusage
conditional
obsolete
attributeusage
预定义特性 attributeusage 描述了如何使用一个自定义特性类。它规定了特性可应用到的项目的类型。
规定该特性的语法如下:
[attributeusage(
validon,
allowmultiple=allowmultiple,
inherited=inherited
)]
其中:
参数 validon 规定特性可被放置的语言元素。它是枚举器 attributetargets 的值的组合。默认值是 attributetargets.all。
参数 allowmultiple(可选的)为该特性的 allowmultiple 属性(property)提供一个布尔值。如果为 true,则该特性是多用的。默认值是 false(单用的)。
参数 inherited(可选的)为该特性的 inherited 属性(property)提供一个布尔值。如果为 true,则该特性可被派生类继承。默认值是 false(不被继承)。
例如:
[attributeusage(attributetargets.class |
attributetargets.constructor |
attributetargets.feild |
attributetargets.method |
attributetargets.property,
allowmultiple = true)]
conditional
这个预定义特性标记了一个条件方法,其执行依赖于它顶的预处理标识符。
它会引起方法调用的条件编译,取决于指定的值,比如 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
这个预定义特性标记了不应被使用的程序实体。它可以让您通知编译器丢弃某个特定的目标元素。例如,当一个新方法被用在一个类中,但是您仍然想要保持类中的旧方法,您可以通过显示一个应该使用新方法,而不是旧方法的消息,来把它标记为 obsolete(过时的)。
规定该特性的语法如下:
[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
创建自定义特性(attribute)
.net 框架允许创建自定义特性,用于存储声明性的信息,且可在运行时被检索。该信息根据设计标准和应用程序需要,可与任何目标元素相关。
创建并使用自定义特性包含四个步骤:
声明自定义特性
构建自定义特性
在目标程序元素上应用自定义特性
通过反射访问特性
最后一个步骤包含编写一个简单的程序来读取元数据以便查找各种符号。元数据是用于描述其他数据的数据和信息。该程序应使用反射来在运行时访问特性。我们将在下一章详细讨论这点。
声明自定义特性
一个新的自定义特性应派生自 system.attribute 类。例如:
// 一个自定义特性 bugfix 被赋给类及其成员
[attributeusage(attributetargets.class |
attributetargets.constructor |
attributetargets.field |
attributetargets.method |
attributetargets.property,
allowmultiple = true)]
public class debuginfo : system.attribute
在上面的代码中,我们已经声明了一个名为 debuginfo 的自定义特性。
构建自定义特性
让我们构建一个名为 debuginfo 的自定义特性,该特性将存储调试程序获得的信息。它存储下面的信息:
bug 的代码编号
辨认该 bug 的开发人员名字
最后一次审查该代码的日期
一个存储了开发人员标记的字符串消息
我们的 debuginfo 类将带有三个用于存储前三个信息的私有属性(property)和一个用于存储消息的公有属性(property)。所以 bug 编号、开发人员名字和审查日期将是 debuginfo 类的必需的定位( positional)参数,消息将是一个可选的命名(named)参数。
每个特性必须至少有一个构造函数。必需的定位( positional)参数应通过构造函数传递。下面的代码演示了 debuginfo 类:
// 一个自定义特性 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;
}
}
}
应用自定义特性
通过把特性放置在紧接着它的目标之前,来应用该特性:
[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());
}
}
以上就是【c#教程】c# 特性(attribute)的内容。