代码之家  ›  专栏  ›  技术社区  ›  Alex

如何判断是否有错误。NET应用程序是在调试模式还是发布模式下编译的?

  •  43
  • Alex  · 技术社区  · 18 年前

    我的电脑上安装了一个应用程序。如何确定它是否在调试模式下编译?

    我试着用 .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 回复  |  直到 7 年前
        1
  •  29
  •   ZombieSheep    18 年前

    我 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;
    }
    
        2
  •  26
  •   Dave Black    8 年前

    僵尸羊的答案是错误的。

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

    要非常小心——只需查看程序集清单中的“assembly attributes”是否存在“debugable”属性就可以了 不 这意味着您有一个未经JIT优化的程序集。程序集可以是JIT优化的,但在高级生成设置下,程序集输出设置为包含“完整”或“仅pdb”信息——在这种情况下,将显示“可调试”属性。

    有关更多信息,请参阅我下面的帖子: How to Tell if an Assembly is Debug or Release 和 How to identify if the DLL is Debug or Release build (in .NET)

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

    你还需要定义 准确地 什么是“调试”与“发布”。。。

    • 你的意思是应用程序配置了代码优化吗?
    • 您的意思是可以将Visual Studio/JIT调试器附加到它吗?
    • 你是说它会生成调试输出吗?
    • 你的意思是它定义了调试常量吗?请记住,可以使用 System.Diagnostics.Conditional() 属性
        3
  •  8
  •   Joe Basirico    18 年前

    事实上,你走的路是对的。如果查看reflector中的反汇编程序窗口,如果它是在调试模式下构建的,则会看到以下行:

    [assembly: Debuggable(...)]
    
        4
  •  2
  •   flipdoubt    17 年前

    用杰夫·基的手机怎么样 IsDebug 公用事业它有点过时,但既然你有了Reflector,你就可以反编译它,并在框架的任何版本中重新编译它。是的。

        5
  •  2
  •   Max    7 年前

    这里是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
    

    使现代化
    这个解决方案对我来说很有效,但正如戴夫·布莱克所指出的,在某些情况下,可能需要一种不同的方法。
    也许你也可以看看戴夫·布莱克的答案: