Search code examples
.netvb.netvisual-studio-2010compiler-directives

Using compiler directives for conditional attributes


I wish to make an attribute conditional based on compilation mode.

For example this is MyFunction() which is decorated with the attribute MyAttribute():

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

However I only want the attribute to be applied when DEBUG is true, which sounds like a great place to use compiler directives:

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

However this seems to require a continuation character ( _) which in turn affects the #End If (unexpected token).

How would I achieve what I want?


Solution

  • Worst case, you'd could do:

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

    This is probably due to the line continuation processing occurring before the pre-processing. For example, this is valid:

    #If 1 = _
      1 Then
    
    #End If
    

    In your case, you are expecting the line continuation to occur after pre-processing.