113

重複の可能性:
.NET アプリケーションが DEBUG モードまたは RELEASE モードでコンパイルされたかどうかを確認する方法は?

これは以前に尋ねられたと確信していますが、GoogleとSOの検索は失敗しました。

DLL がリリース ビルドかデバッグ ビルドかを特定するにはどうすればよいですか?

4

2 に答える 2

117

私見、上記のアプリケーションは本当に誤解を招くものです。コードが最適化および JIT 最適化のためにコンパイルされているかどうかに完全に依存しない IsJITTrackingEnabled のみを検索します。

Release モードでコンパイルし、DebugOutput を「none」以外に選択した場合、DebuggableAttribute が存在します。

また、「デバッグ」と「リリース」の意味を正確に定義する必要があります...

アプリがコード最適化で構成されているということですか? VS/JIT Debugger をアタッチできるということですか?DebugOutputを生成するということですか?DEBUG定数を定義しているということですか?System.Diagnostics.Conditional() 属性を使用してメソッドを条件付きでコンパイルできることに注意してください。

私見、誰かがアセンブリが「デバッグ」または「リリース」であるかどうかを尋ねるとき、彼らは本当にコードが最適化されているかどうかを意味します...

これを手動で行いますか、それともプログラムで行いますか?

手動: アセンブリのメタデータの DebuggableAttribute ビットマスクの値を表示する必要があります。方法は次のとおりです。

  1. ILDASM でアセンブリを開きます
  2. マニフェストを開く
  3. DebuggableAttribute ビットマスクを見てください。DebuggableAttribute が存在しない場合、それは間違いなく最適化されたアセンブリです。
  4. 存在する場合は、4 番目のバイトを見てください。「0」の場合は JIT 最適化されています。それ以外の場合は、そうではありません。

// メタデータ バージョン: v4.0.30319 .... // .custom インスタンス void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 02 00 00 00 00 00 )

プログラムで : コードが JITOptimized かどうかをプログラムで知りたいと仮定すると、正しい実装は次のとおりです (単純なコンソール アプリで)。

void Main()
{
    var HasDebuggableAttribute = false;
    var IsJITOptimized = false;
    var IsJITTrackingEnabled = false;
    var BuildType = "";
    var DebugOutput = "";
    
    var ReflectedAssembly = Assembly.LoadFile(@"path to the dll you are testing");
    object[] attribs = ReflectedAssembly.GetCustomAttributes(typeof(DebuggableAttribute), false);

    // If the 'DebuggableAttribute' is not found then it is definitely an OPTIMIZED build
    if (attribs.Length > 0)
    {
        // Just because the 'DebuggableAttribute' is found doesn't necessarily mean
        // it's a DEBUG build; we have to check the JIT Optimization flag
        // i.e. it could have the "generate PDB" checked but have JIT Optimization enabled
        DebuggableAttribute debuggableAttribute = attribs[0] as DebuggableAttribute;
        if (debuggableAttribute != null)
        {
            HasDebuggableAttribute = true;
            IsJITOptimized = !debuggableAttribute.IsJITOptimizerDisabled;
            
            // IsJITTrackingEnabled - Gets a value that indicates whether the runtime will track information during code generation for the debugger.
            IsJITTrackingEnabled = debuggableAttribute.IsJITTrackingEnabled;
            BuildType = debuggableAttribute.IsJITOptimizerDisabled ? "Debug" : "Release";

            // check for Debug Output "full" or "pdb-only"
            DebugOutput = (debuggableAttribute.DebuggingFlags &
                            DebuggableAttribute.DebuggingModes.Default) !=
                            DebuggableAttribute.DebuggingModes.None
                            ? "Full" : "pdb-only";
        }
    }
    else
    {
        IsJITOptimized = true;
        BuildType = "Release";
    }

    Console.WriteLine($"{nameof(HasDebuggableAttribute)}: {HasDebuggableAttribute}");
    Console.WriteLine($"{nameof(IsJITOptimized)}: {IsJITOptimized}");
    Console.WriteLine($"{nameof(IsJITTrackingEnabled)}: {IsJITTrackingEnabled}");
    Console.WriteLine($"{nameof(BuildType)}: {BuildType}");
    Console.WriteLine($"{nameof(DebugOutput)}: {DebugOutput}");
}

この実装は、次のブログで提供しています。

アセンブリがデバッグかリリースかを確認する方法

于 2011-03-15T18:55:54.403 に答える
95

これを行う唯一の最善の方法は、コンパイルされたアセンブリ自体をチェックすることです。Rotem Bloomがここで見つけた「.NET Assembly Information」という非常に便利なツールがあります。これをインストールすると、自分自身を .dll ファイルに関連付けて自分自身で開きます。インストール後、アセンブリをダブルクリックして開くと、下のスクリーンショットに示すようにアセンブリの詳細が表示されます。そこで、デバッグがコンパイルされているかどうかを識別できます。

お役に立てれば..

于 2009-04-28T17:15:19.337 に答える