2009-10-02 165 views
14

为什么#IF Not DEBUG不按我期望的方式在VB.NET中工作?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 D = True 
#If D Then 
    Console.WriteLine("D") 
#End If 

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

,当然,C#有预期的行为,以及:

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

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

正常工作对我来说,第一次只显示调试在调试模式,而不是调试,而不是在释放模式调试。你确定你的项目设置中没有“更奇怪”的东西吗? – 2009-10-02 19:08:33

+0

嗯......我已经在现有的ASP.NET项目中使用VS2008尝试了它,然后使用代码片段编译器。我会尝试一个新的控制台项目,看看会发生什么。 – 2009-10-02 19:18:21

+0

这是我尝试过的一个新的控制台应用程序。 – 2009-10-02 19:19:12

回答

10

事实证明,这不是所有破坏的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! 

这是有道理的......我没有定义调试,所以它显示“不调试!”。

> 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,所以它显示“不调试”。但是,如果我包含调试符号呢?

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,must define it the same way

看起来像CodeDomProvider绊倒了VB.NET的愚蠢的psuedo-logical operators

故事的道德?对于VB.NET,#If Not不是一个好主意。


而现在,源可用,我可以verify that it does actually set it equal to 1如我所料:

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