ConditionalAttribute 类

定义

指示编译器,除非定义了指定的有条件编译符号,否则,应忽略方法调用或属性。

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

示例

以下示例演示了 ConditionalAttribute 的用法。 该示例假定条件是使用 /define 编译器选项定义的。 可以通过更改编译器选项来获取不同的结果。 你可以选择通过使用代码示例中的杂注而不是将这些杂注标识为编译器选项来定义条件。

#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 方法和类。 但是,它对类的使用仅适用于派生自 Attribute的类型。 ConditionalAttribute 如果将其应用于任何其他类型,将忽略或生成编译器警告或错误消息。

ConditionalAttribute应用于方法指示,除非定义了与ConditionalAttribute关联的条件编译符号,否则不应将调用该方法编译为 Microsoft 中间语言 (MSIL) 。 如果您将此特性应用于不返回 void 的方法,则将在 Visual Studio 中收到编译错误。 ConditionalAttribute应用于属性表示,除非定义了条件编译符号,否则不应向元数据发出该属性。 传递给方法或属性的任何参数仍由编译器进行类型检查。

可以使用以下技术来定义条件编译符号:

  • 使用编译器命令行选项;例如 ,/define:DEBUG

  • 在操作系统 shell 中使用环境变量;例如, 设置 DEBUG=1

  • 在源代码中使用杂注;例如,定义编译变量,如下所示:

    #define DEBUG  
    
    #Const DEBUG=True  
    

    若要取消定义变量,请使用下列内容:

    #undef DEBUG  
    
    #Const DEBUG=False  
    

允许忽略 ConditionalAttribute符合公共语言规范 (CLS) 的编译器。 C#、F#、Visual Basic 和 C++ 编译器支持ConditionalAttribute;JScript编译器不支持该属性。

备注

在 Visual Basic 中,AddressOf 运算符不受此特性的影响。 例如,Call CType(AddressOf delegate, Action) 始终调用 delegate,但 Call delegate() 不会执行此操作。

ConditionalAttribute应用于在类TraceDebug定义的方法和类。

有关如何使用属性的详细信息,请参阅 “属性”。

构造函数

ConditionalAttribute(String)

初始化 ConditionalAttribute 类的新实例。

属性

ConditionString

获取与 ConditionalAttribute 属性相关的条件编译符号。

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。

(继承自 Attribute)
GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

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

将一组名称映射为对应的一组调度标识符。

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

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

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

检索对象提供的类型信息接口的数量(0 或 1)。

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

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于