VB.NET 预处理器指令 [英] VB.NET Preprocessor Directives
问题描述
为什么 #IF Not DEBUG
不能像我在 VB.NET 中期望的那样工作?
Why doesn't #IF Not DEBUG
work the way I'd expect in 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 可以:
But, a manually set const does:
#Const D = True
#If D Then
Console.WriteLine("D")
#End If
#If Not D Then
Console.WriteLine("Not D")
#End If
' Outputs: D
当然,C# 也有预期的行为:
And, of course, C# has the expected behavior as well:
#if DEBUG
Console.WriteLine("Debug");
#endif
#if !DEBUG
Console.WriteLine("Not Debug");
#endif
// Outputs: Debug
推荐答案
事实证明,损坏的并不是 VB.NET 的全部 - 只是 CodeDomProvider(ASP.NET 和 Snippet Compiler 都使用它)).
Turns out, it's not all of VB.NET that's broken - just the CodeDomProvider (which both ASP.NET and Snippet Compiler use).
给定一个简单的源文件:
Given a simple source file:
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) 编译:
Compiling with vbc.exe version 9.0.30729.1 (.NET FX 3.5):
> vbc.exe default.vb /out:out.exe
> out.exe
Not Debug!
这是有道理的...我没有定义 DEBUG,所以它显示Not Debug!".
That makes sense...I didn't define DEBUG, so it shows "Not Debug!".
> vbc.exe default.vb /out:out.exe /debug:full
> out.exe
Not Debug!
并且,使用 CodeDomProvider:
And, using 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".但是,如果我包含调试符号怎么办?
Okay, again - that makes sense. I didn't define DEBUG, so it shows "Not Debug". But, what if I include debug symbols?
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,必须以同样的方式定义.
Hmm...I didn't define DEBUG, but maybe it defined it for me? But if it did, it must have defined it as "1" - because I can't get that behavior with any other value. ASP.NET, using the CodeDomProvider, must define it the same way.
看起来 CodeDomProvider 正在绊倒 VB.NET 愚蠢的伪逻辑运算符一>.
Looks like the CodeDomProvider is tripping over VB.NET's stupid psuedo-logical operators.
故事的寓意?#If Not
对 VB.NET 来说不是一个好主意.
Moral of the story? #If Not
is not a good idea for VB.NET.
现在该源可用,我可以验证它确实按照我的预期将它设置为等于 1:
And now that source is available, I can verify that it does actually set it equal to 1 as I expected:
if (options.IncludeDebugInformation) {
sb.Append("/D:DEBUG=1 ");
sb.Append("/debug+ ");
}
这篇关于VB.NET 预处理器指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!