【问题标题】:How to tell if a .NET application was compiled in DEBUG or RELEASE mode?如何判断 .NET 应用程序是在 DEBUG 还是 RELEASE 模式下编译的?
【发布时间】:2010-09-16 17:09:09
【问题描述】:

我的电脑上安装了一个应用程序。如何判断它是否在 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

【问题讨论】:

标签: .net executable debug-symbols compiler-options


【解决方案1】:

blogged这是很久以前的事了,我不知道它是否仍然有效,但代码类似于......

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

【讨论】:

  • 正如我所说,那是几年前我做调查工作的时候......谁知道框架发生了什么变化,甚至我最初的工作有多糟糕。 ;)
  • 精彩的代码!我创建了一个集成在我的构建机器中的控制台应用程序,以验证要交付的最终应用程序不会处于调试模式。
  • 请在下面查看我的回答,了解为什么这种检查 DEBUG 构建的方法不是最佳解决方案。
  • 仅供参考 - 我刚刚使用 Visual Studio 2013 构建了相同 dll 的调试和发布版本,并运行了上述方法(使用 LINQPad),两次脚本都认为有问题的 DLL 是发布版本。跨度>
【解决方案2】:

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

[assembly: Debuggable(...)]

【讨论】:

  • 不正确;我刚刚检查了一个我在发布模式下构建的程序集。它仍然有这个属性:[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
  • 我在发布模式下编译,我在反射器中看到了这个:[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
  • 当我在调试模式下反汇编程序集时,我看到:[程序集:Debuggable(DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.EnableEditAndContinue | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.Default) ] 当我在发布模式下构建它时,debuggable 属性只有 IgnoreSymbolStoreSequencePoints 标志,正如你们所提到的。
  • 您的答案不正确 - 如果您在发布模式下编译并将 DebugOutput 选择为“none”以外的任何内容,则会出现 DebuggableAttribute。
【解决方案3】:

使用 Jeff Key 的 IsDebug 实用程序怎么样?它有点过时了,但由于你有 Reflector,你可以反编译它并在任何版本的框架中重新编译它。我做到了。

【讨论】:

【解决方案4】:

ZombieSheep 的回答不正确。

我对这个重复问题的回答在这里:How to tell if a .NET application was compiled in DEBUG or RELEASE mode?

要非常小心 - 仅查看程序集清单中的“程序集属性”是否存在“可调试”属性不是意味着您有一个未经过 JIT 优化的程序集。程序集可以进行 JIT 优化,但将高级构建设置下的程序集输出设置为包含“完整”或“仅 pdb”信息 - 在这种情况下,将存在“可调试”属性。

请参阅下面的帖子以获取更多信息: How to Tell if an Assembly is Debug or ReleaseHow to identify if the DLL is Debug or Release build (in .NET)

Jeff Key 的应用程序无法正常工作,因为它根据 DebuggableAttribute 是否存在来识别“调试”构建。如果您在 Release 模式下编译并选择 DebugOutput 为“none”以外的任何内容,则会出现 DebuggableAttribute。

您还需要定义exaclty“调试”与“发布”的含义...

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

【讨论】:

  • 感谢您提供有关“调试构建”的不同方面和类型的详细说明。下次我看到一些不确定的实现时,我一定会把人们引向你的博客。
【解决方案5】:

这里是ZombieSheep提出的解决方案的VB.Net版本

Public Shared Function IsDebug(Assem As [Assembly]) As Boolean
    For Each attrib In Assem.GetCustomAttributes(False)
        If TypeOf attrib Is System.Diagnostics.DebuggableAttribute Then
            Return DirectCast(attrib, System.Diagnostics.DebuggableAttribute).IsJITTrackingEnabled
        End If
    Next

    Return False
End Function

Public Shared Function IsThisAssemblyDebug() As Boolean
    Return IsDebug([Assembly].GetCallingAssembly)
End Function

更新
这个解决方案对我有用,但正如 Dave Black 指出的那样,可能存在需要不同方法的情况。
所以也许你也可以看看 Dave Black 的回答:

【讨论】:

猜你喜欢
  • 2013-03-21
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
相关资源
最近更新 更多