VB.NET预处理器指令
为什么VB.NET预处理器指令,vb.net,preprocessor,Vb.net,Preprocessor,为什么#如果不调试,就不能像我在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 但是,手动设置的常
#如果不调试
,就不能像我在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
事实证明,并不是所有的VB.NET都坏了——只是CodeDomProvider(ASP.NET和代码段编译器都使用它)
给定一个简单的源文件:
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)编译:
这是有道理的…我没有定义DEBUG,所以它显示“notdebug!”
并且,使用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,所以它显示“notdebug”。但是,如果我包含调试符号呢
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!
嗯…我没有定义调试,但也许它为我定义了它?但如果是这样的话,它一定把它定义为“1”-因为我不能用任何其他值来获得该行为。ASP.NET,使用CodeDomProvider
看起来CodeDomProvider被VB.NET的愚蠢绊倒了
这个故事的寓意是什么#如果不是
对VB.NET来说不是个好主意
现在该来源可用,我可以如我所料:
if (options.IncludeDebugInformation) {
sb.Append("/D:DEBUG=1 ");
sb.Append("/debug+ ");
}
对我来说效果很好,第一个在调试模式下显示just Debug,在发布模式下显示Not Debug,Not Debug。你确定你的项目设置中没有出现“wierd”吗?嗯……我在现有的ASP.NET项目中用VS2008和Snippet编译器都试过了。我将尝试一个新的控制台项目,看看会发生什么。我尝试了一个新的控制台应用程序。Argh。新的控制台应用程序按预期工作。现在我真的很困惑…奇怪。确定没有人弄乱了项目的预处理器设置?
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!
if (options.IncludeDebugInformation) {
sb.Append("/D:DEBUG=1 ");
sb.Append("/debug+ ");
}