Dot-Net

如何辨識 DLL 是 Debug 還是 Release 建構(在 .NET 中)[重複]

  • April 28, 2009

可能重複:

如何判斷 .NET 應用程序是在 DEBUG 還是 RELEASE 模式下編譯的?

我確定以前有人問過這個問題,但是Google和 SO 搜尋讓我失敗了。

如何辨識 DLL 是發布版本還是調試版本?

唯一最好的方法是檢查編譯的程序集本身。Rotem Bloom在這裡找到了一個非常有用的工具,稱為“.NET 程序集資訊” 。安裝後,它會將自身與 .dll 文件關聯以自行打開。安裝後,您只需點兩下程序集即可打開,它將為您提供程序集詳細資訊,如下面的螢幕截圖所示。在那裡您可以確定它是否已調試編譯。

希望這可以幫助..

恕我直言,上述應用程序確實具有誤導性;它只查找完全獨立於程式碼是否為優化和 JIT 優化而編譯的 IsJITTrackingEnabled。

如果您在發布模式下編譯並將 DebugOutput 選擇為“none”以外的任何內容,則會出現 DebuggableAttribute。

您還需要準確定義“調試”與“發布”的含義……

你的意思是應用程序配置了程式碼優化?您的意思是您可以將 VS/JIT 調試器附加到它嗎?你的意思是它會生成DebugOutput嗎?你的意思是它定義了 DEBUG 常量嗎?請記住,您可以使用 System.Diagnostics.Conditional() 屬性有條件地編譯方法。

恕我直言,當有人詢問程序集是“調試”還是“發布”時,他們的真正意思是程式碼是否經過優化……

Sooo,您想手動還是以程式方式執行此操作?

手動:您需要查看程序集元數據的 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)

以程式方式:假設您想以程式方式了解程式碼是否經過 JIT 優化,這是正確的實現(在簡單的控制台應用程序中):

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}");
}

我在我的部落格上提供了這個實現:

如何判斷程序集是調試還是發布

引用自:https://stackoverflow.com/questions/798971