1、AttributeUsage(AttributeUsageAttribute 类):用来控制自定义特性的使用。
[Conditional(conditionalSymbol)]
例:
[AttributeUsage(System.AttributeTargets.All, AllowMultiple = false, Inherited = true)]
参数解析:
validon :特性可被应用到的语言元素。来源于枚举器 AttributeTargets 的值的组合,多个值之间使用”|“连接。默认值是 AttributeTargets.All 。AttributeTargets枚举值 | 说明 | |
All | 可以对任何应用程序元素应用属性。 | |
Assembly | 可以对程序集应用属性。 | |
Class | 可以对类应用属性。 | |
Constructor | 可以对构造函数应用属性。 | |
Delegate | 可以对委托应用属性。 | |
Enum | 可以对枚举应用属性。 | |
Event | 可以对事件应用属性。 | |
Field | 可以对字段应用属性。 | |
GenericParameter | 可以对泛型参数应用属性。 | |
| 可以对接口应用属性。 | |
Method | 可以对方法应用属性。 | |
| 可以对模块应用属性。 | |
Parameter | 可以对参数应用属性。 | |
Property | 可以对属性 (Property) 应用属性 (Attribute)。 | |
ReturnValue | 可以对返回值应用属性。 | |
Struct | 可以对结构应用属性,即值类型。 |
AllowMultiple:特性是否可对单个实体应用多次。(可选项)。默认值是 false。
Inherited:特性是否可被派生类继承。(可选项)。默认值是 false。
例如:
[AttributeUsage(AttributeTargets.Constructor |AttributeTargets.Field |AttributeTargets.Method,AllowMultiple = true,Inherited = true)]
[AttributeUsage(AttributeTargets.Constructor |AttributeTargets.Field |AttributeTargets.Method,AllowMultiple = true,Inherited = true)]
2、Conditional(ConditionalAttribute 类):据预处理标识符执行方法;封闭#if 和 #endif 内部方法的替代方法;可应用于方法、类。
[Conditional(conditionalSymbol)]
例:
#define CONDITION1
#define CONDITION2
using System;
using System.Diagnostics;
class Test
{
static void Main()
{
Console.WriteLine("Calling Method1");
Method1(3);
Console.WriteLine("Calling Method2");
Method2();
Console.WriteLine("Using the Debug class");
Debug.Listeners.Add(new ConsoleTraceListener());
Debug.WriteLine("DEBUG is defined");
}
[Conditional("CONDITION1")]
public static void Method1(int x)
{
Console.WriteLine("CONDITION1 is defined");
}
[Conditional("CONDITION1"), Conditional("CONDITION2")]
public static void Method2()
{
Console.WriteLine("CONDITION1 or CONDITION2 is defined");
}
}
/*
When compiled as shown, the application (named ConsoleApp)
produces the following output.
Calling Method1
CONDITION1 is defined
Calling Method2
CONDITION1 or CONDITION2 is defined
Using the Debug class
DEBUG is defined
*/
3、Obsolete(ObsoleteAttribute 类):标记不再使用的程序元素;适用于类、结构、枚举、构造函数、方法、属性、字段、事件、接口、委托。
[Obsolete(Message)]
[Obsolete(Message,IsError)]
[Obsolete(Message)]
[Obsolete(Message,IsError)]
参数解析:
Message:获取变通方法消息,包括对可选程序元素的说明;即对使用该元素时的提示信息。
IsError:编译器编译的时候是否将已过时的程序元素视为错误;true将使用已过时的元素视为错误;false将使用已过时的元素视为警告。默认值是 false。
例:
Message:获取变通方法消息,包括对可选程序元素的说明;即对使用该元素时的提示信息。
IsError:编译器编译的时候是否将已过时的程序元素视为错误;true将使用已过时的元素视为错误;false将使用已过时的元素视为警告。默认值是 false。
例:
using System;
using System.Reflection;
public class Example
{
// Mark OldProperty As Obsolete.
[ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", false)]
public string OldProperty
{ get { return "The old property value."; } }
public string NewProperty
{ get { return "The new property value."; } }
// Mark OldMethod As Obsolete.
[ObsoleteAttribute("This method is obsolete. Call NewMethod instead.", true)]
public string OldMethod()
{
return "You have called OldMethod.";
}
public string NewMethod()
{
return "You have called NewMethod.";
}
public static void Main()
{
// Get all public members of this type.
MemberInfo[] members = typeof(Example).GetMembers();
// Count total obsolete members.
int n = 0;
// Try to get the ObsoleteAttribute for each public member.
Console.WriteLine("Obsolete members in the Example class:\n");
foreach (var member in members) {
ObsoleteAttribute[] attribs = (ObsoleteAttribute[])
member.GetCustomAttributes(typeof(ObsoleteAttribute),
false);
if (attribs.Length > 0) {
ObsoleteAttribute attrib = attribs[0];
Console.WriteLine("Member Name: {0}.{1}", member.DeclaringType.FullName, member.Name);
Console.WriteLine(" Message: {0}", attrib.Message);
Console.WriteLine(" Warning/Error: {0}", attrib.IsError ? "Error" : "Warning");
n++;
}
}
if (n == 0)
Console.WriteLine("The Example type has no obsolete attributes.");
}
}
// The example displays the following output:
// Obsolete members in the Example class:
//
// Member Name: Example.OldMethod
// Message: This method is obsolete. Call NewMethod instead.
// Warning/Error: Error
// Member Name: Example.OldProperty
// Message: This property is obsolete. Use NewProperty instead.
// Warning/Error: Warning