15

#IF Not DEBUGVB.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

もちろん、C# にも期待どおりの動作があります。

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

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

1 に答える 1

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!

それは理にかなっています...私は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」と定義したに違いありません。他の値ではその動作を取得できないためです。CodeDomProvider を使用する ASP.NET は、同じ方法で定義する必要があります

CodeDomProvider が VB.NET の愚かな疑似論理演算子につまずいているようです。

この話の教訓?#If NotVB.NET には適していません。


ソースが利用可能になったので、期待どおりに実際に 1 に設定されていることを確認できます。

if (options.IncludeDebugInformation) {
      sb.Append("/D:DEBUG=1 ");
      sb.Append("/debug+ ");
}
于 2009-10-02T20:41:06.700 に答える