Директивы Препроцессора VB.NET

Почему #IF Not DEBUG работает так, как я ожидал бы в VB.NET?

#If DEBUG Then
   Console.WriteLine("Debug")
#End If

#If Not DEBUG Then
   Console.WriteLine("Not Debug")
#End If

#If DEBUG = False Then
   Console.WriteLine("Not Debug")
#End If
' Outputs: Debug, Not Debug

Но, заданная вручную команда const:

#Const D = True
#If D Then
   Console.WriteLine("D")
#End If

#If Not D Then
   Console.WriteLine("Not D")
#End If
' Outputs: D

И, конечно же, С# также имеет ожидаемое поведение:

#if DEBUG
    Console.WriteLine("Debug");
#endif

#if !DEBUG
    Console.WriteLine("Not Debug");
#endif
// Outputs: Debug

Ответы

Ответ 1

Оказывается, это не все из VB.NET, которое сломалось - просто CodeDomProvider (который использует как ASP.NET, так и Snippet Compiler).

Учитывая простой исходный файл:

Imports System
Public Module Module1
    Sub Main()
       #If DEBUG Then
          Console.WriteLine("Debug!")
       #End If

       #If Not DEBUG Then
          Console.WriteLine("Not Debug!")
       #End If
    End Sub
End Module

Компиляция с помощью vbc.exe версии 9.0.30729.1 (.NET FX 3.5):

> vbc.exe default.vb /out:out.exe
> out.exe
  Not Debug!

Это имеет смысл... Я не определял DEBUG, поэтому он показывает "Not Debug!".

> vbc.exe default.vb /out:out.exe /debug:full
> out.exe
  Not Debug!

И, используя CodeDomProvider:

Using p = CodeDomProvider.CreateProvider("VisualBasic")
   Dim params As New CompilerParameters() With { _
      .GenerateExecutable = True, _
      .OutputAssembly = "out.exe" _
   }
   p.CompileAssemblyFromFile(params, "Default.vb")
End Using

> out.exe
Not Debug!

Хорошо, снова - это имеет смысл. Я не определял DEBUG, поэтому он показывает "Not Debug". Но что, если я включаю отладочные символы?

Using p = CodeDomProvider.CreateProvider("VisualBasic")
   Dim params As New CompilerParameters() With { _
      .IncludeDebugInformation = True, _
      .GenerateExecutable = True, _
      .OutputAssembly = "C:\Users\brackett\Desktop\out.exe" _
   }
   p.CompileAssemblyFromFile(params, "Default.vb")
End Using

> out.exe
Debug!
Not Debug!

Хм... Я не определял DEBUG, но, возможно, он определил его для меня? Но если это так, оно должно определить его как "1" - потому что я не могу получить это поведение с любым другим значением. ASP.NET, используя CodeDomProvider, должен определить его таким же образом.

Похоже, что CodeDomProvider отключается от VB.NET stupid псевдо-логических операторов.

Мораль истории? #If Not не является хорошей идеей для VB.NET.


И теперь, когда источник доступен, я могу убедиться, что он действительно устанавливает его равным 1, как я ожидал:

if (options.IncludeDebugInformation) {
      sb.Append("/D:DEBUG=1 ");
      sb.Append("/debug+ ");
}