如何判断.NET应用程序是否在DEBUG或RELEASE模式下编译?

45 .net executable debug-symbols compiler-options

我的计算机上安装了一个应用程序.如何确定它是否在DEBUG模式下编译?

我曾尝试使用.NET Reflector,但它没有显示任何具体内容.这是我看到的:

// Assembly APPLICATION_NAME, Version 8.0.0.15072
Location: C:\APPLICATION_FOLDER\APPLICATION_NAME.exe
Name: APPLICATION_NAME, Version=8.0.0.15072, Culture=neutral, PublicKeyToken=null
Type: Windows Application
Run Code Online (Sandbox Code Playgroud)

Zom*_*eep 29

我很久以前在博客上发帖,我不知道它是否仍然有效,但代码是......

private void testfile(string file)
{
    if(isAssemblyDebugBuild(file))
    {
        MessageBox.Show(String.Format("{0} seems to be a debug build",file));
    }
    else
    {
        MessageBox.Show(String.Format("{0} seems to be a release build",file));
    }
}    

private bool isAssemblyDebugBuild(string filename)
{
    return isAssemblyDebugBuild(System.Reflection.Assembly.LoadFile(filename));    
}    

private bool isAssemblyDebugBuild(System.Reflection.Assembly assemb)
{
    bool retVal = false;
    foreach(object att in assemb.GetCustomAttributes(false))
    {
        if(att.GetType() == System.Type.GetType("System.Diagnostics.DebuggableAttribute"))
        {
            retVal = ((System.Diagnostics.DebuggableAttribute)att).IsJITTrackingEnabled;
        }
    }
    return retVal;
}
Run Code Online (Sandbox Code Playgroud)

  • 请参阅下面的答案,了解为什么这种检查DEBUG构建的方法不是最佳解决方案. (3认同)

Dav*_*ack 26

ZombieSheep的回答是不正确的.

我对这个重复问题的回答如下:如何判断.NET应用程序是在DEBUG还是RELEASE模式下编译的?

要非常小心-只是在"程序集属性"在大会清单为"可调试"属性的存在确实意味着你有没有JIT优化的装配.程序集可以进行JIT优化,但将Advanced Build设置下的Assembly Output设置为包含'full'或'pdb-only'信息 - 在这种情况下,将出现'Debuggable'属性.

有关更多信息,请参阅下面的帖子: 如何判断程序集是否为调试或发布以及 如何识别DLL是否为Debug或Release版本(在.NET中)

Jeff Key的应用程序无法正常工作,因为它根据DebuggableAttribute是否存在来识别"Debug"构建.如果在Release模式下编译并选择DebugOutput为"none"以外的任何值,则存在DebuggableAttribute.

您还需要定义exaclty "Debug"与"Release"的含义...

  • 您是说应用程序配置了代码优化?
  • 你的意思是你可以附加Visual Studio/JIT调试器吗?
  • 你的意思是它生成DebugOutput?
  • 你是说它定义了DEBUG常量吗?请记住,您可以使用该System.Diagnostics.Conditional()属性有条件地编译方法.


Joe*_*ico 9

你实际上是在正确的道路上.如果您在反射器中查看反汇编程序窗口,如果它是在调试模式下构建的,您将看到以下行:

[assembly: Debuggable(...)]
Run Code Online (Sandbox Code Playgroud)

  • 不对; 我刚刚检查了我在发布模式下构建的程序集.它仍然具有以下属性:[assembly:Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] (8认同)
  • 当我在调试模式下反汇编程序集时,我看到:[assembly:Debuggable(DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.EnableEditAndContinue | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.Default)]当我在发布模式下构建它时,你们都提到,debuggable属性只有IgnoreSymbolStoreSequencePoints标志. (5认同)
  • 我在发布模式下编译,我在反射器中看到了这个:[assembly:Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] (3认同)