ConditionalAttribute 类

定义

指示编译器,除非定义了指定的有条件编译符号,否则,应忽略方法调用或属性。Indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.

public ref class ConditionalAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=true)]
[System.Serializable]
public sealed class ConditionalAttribute : Attribute
type ConditionalAttribute = class
    inherit Attribute
Public NotInheritable Class ConditionalAttribute
Inherits Attribute
继承
ConditionalAttribute
属性

示例

以下示例演示了 ConditionalAttribute 的用法。The following example demonstrates the use of ConditionalAttribute. 该示例假设条件是用 /define编译器选项定义的。The example assumes that the condition is defined with the /define compiler option. 可以通过更改编译器选项来获取不同的结果。You can obtain different results by changing the compiler option. 你可以选择通过使用代码示例中的杂注而不是将这些杂注标识为编译器选项来定义条件。You can optionally define the conditions by using pragmas in the sample code instead of identifying them as compiler options.

#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
*/
#Const CONDITION1 = True
#Const CONDITION2 = True
Imports System.Diagnostics

Class Test

    Shared Sub 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")
    End Sub
       
    <ConditionalAttribute("CONDITION1")> _
    Shared Sub Method1(x As Integer)
        Console.WriteLine("CONDITION1 is defined")
    End Sub
    
    <ConditionalAttribute("CONDITION1"), ConditionalAttribute("CONDITION2")> _
    Shared Sub Method2()
        Console.WriteLine("CONDITION1 or CONDITIOIN2 is defined")
    End Sub
    
End Class


' 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

注解

可以将ConditionalAttribute属性应用于方法和类。You can apply the ConditionalAttribute attribute to methods and classes. 但是, 对类的使用只对从Attribute派生的类型有效。However, its use on classes is valid only for types that are derived from Attribute. ConditionalAttribute如果将它应用于任何其他类型, 则将被忽略或将生成编译器警告或错误消息。ConditionalAttribute either will be ignored or will produce a compiler warning or error message if you apply it to any other type.

如果ConditionalAttribute应用于方法, 则指示编译器不应将对该方法的调用编译为 Microsoft 中间语言 (MSIL), 除非定义了与ConditionalAttribute关联的条件编译符号。Applying ConditionalAttribute to a method indicates to compilers that a call to the method should not be compiled into Microsoft intermediate language (MSIL) unless the conditional compilation symbol that is associated with ConditionalAttribute is defined. 如果您将此特性应用于不返回 void 的方法,则将在 Visual Studio 中收到编译错误。You will get a compilation error in Visual Studio if you apply this attribute to a method that does not return void. 如果ConditionalAttribute应用于属性, 则指示不应将该属性发送到元数据, 除非定义条件编译符号。Applying ConditionalAttribute to an attribute indicates that the attribute should not be emitted to metadata unless the conditional compilation symbol is defined. 传递给方法或属性的任何参数仍由编译器进行类型检查。Any arguments passed to the method or attribute are still type-checked by the compiler.

您可以使用以下方法定义条件编译符号:You can use the following techniques to define conditional compilation symbols:

  • 使用编译器命令行选项;例如, /define: DEBUGUse compiler command-line options; for example, /define:DEBUG.

  • 使用操作系统 shell 中的环境变量;例如,设置 DEBUG = 1Use environment variables in the operating system shell; for example, set DEBUG=1.

  • 在源代码中使用杂注;例如,定义编译变量,如下所示:Use pragmas in the source code; for example, define the compilation variable as follows:

    #define DEBUG  
    
    #Const DEBUG=True  
    

    若要取消定义变量,请使用下列内容:To undefine the variable, use the following:

    #undef DEBUG  
    
    #Const DEBUG=False  
    

允许使用符合公共语言规范 (CLS) 的编译器忽略ConditionalAttributeCompilers that comply with the Common Language Specification (CLS) are permitted to ignore ConditionalAttribute. C#、Visual Basic 和 C++ 编译器支持 ConditionalAttribute;JScript 编译器不支持此特性。The C#, Visual Basic, and C++ compilers support ConditionalAttribute; the JScript compiler does not support the attribute.

备注

在 Visual Basic 中,AddressOf 运算符不受此特性的影响。In Visual Basic, the AddressOf operator is not affected by this attribute. 例如,Call CType(AddressOf delegate, Action) 始终调用 delegate,但 Call delegate() 不会执行此操作。For example, Call CType(AddressOf delegate, Action) always invokes delegate, although Call delegate() might not.

ConditionalAttribute应用于DebugTrace类中定义的方法。ConditionalAttribute is applied to the methods that are defined in the Debug and Trace classes.

有关如何使用特性的详细信息, 请参阅特性For more information about how to use attributes, see Attributes.

构造函数

ConditionalAttribute(String)

初始化 ConditionalAttribute 类的新实例。Initializes a new instance of the ConditionalAttribute class.

属性

ConditionString

获取与 ConditionalAttribute 属性相关的条件编译符号。Gets the conditional compilation symbol that is associated with the ConditionalAttribute attribute.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式界面实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)

适用于