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

时间:2008-10-11 20:47:04

标签: .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

5 个答案:

答案 0 :(得分:29)

很久以前我就是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;
}

答案 1 :(得分:25)

ZombieSheep的回答不正确。

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

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

有关详细信息,请参阅下面的帖子: 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是否存在来识别“Debug”构建。如果在Release模式下编译并选择DebugOutput为“none”以外的任何值,则存在DebuggableAttribute。

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

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

答案 2 :(得分:9)

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

[assembly: Debuggable(...)]

答案 3 :(得分:2)

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

答案 4 :(得分:2)

以下是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的答案:

相关问题