【问题标题】:Using compiler directives for conditional attributes对条件属性使用编译器指令
【发布时间】:2013-01-17 14:53:21
【问题描述】:

我希望根据编译模式使属性成为条件。

例如这是MyFunction(),它用属性MyAttribute()装饰:

<MyAttribute()>  
Private Function MyFunction() As Boolean
....
End Function

但是我希望在 DEBUG 为真时应用该属性,这听起来是一个使用 compiler directives 的好地方:

#If Debug Then
<MyAttribute()>
#End If
Private Function MyFunction() As Boolean
....
End Function

然而,这似乎需要一个连续字符 (_),这反过来会影响 #End If(意外标记)。

我将如何实现我想要的?

【问题讨论】:

    标签: .net vb.net visual-studio-2010 compiler-directives


    【解决方案1】:

    最坏的情况,你可以这样做:

    #If Debug Then
    <MyAttribute()>
    Private Function MyFunction() As Boolean
    #Else
    Private Function MyFunction() As Boolean
    #End If
    

    这可能是由于在预处理之前发生了行继续处理。例如,这是有效的:

    #If 1 = _
      1 Then
    
    #End If
    

    在您的情况下,您希望在预处理后继续行。

    【讨论】:

    • 那么这是编译器的错误吗?
    • @m.edmondson - 用我对您的评论的回答更新了我的回答 ;-)
    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多