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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/194616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:18:36  来源:igfitidea点击:

How to tell if a .NET application was compiled in DEBUG or RELEASE mode?

.netexecutabledebug-symbolscompiler-options

提问by

I have an application installed on my computer. How do I find out if it was compiled in DEBUG mode or not?

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

I've tried to use .NET Reflector, but it does not show anything specific. Here is what I see:

我尝试使用.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

回答by ZombieSheep

I bloggedthis a long time ago, and I don't know if it still valid or not, but the code is something like...

我很久以前在博客上写过这个,我不知道它是否仍然有效,但是代码是这样的......

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

回答by Dave Black

ZombieSheep's answer is incorrect.

ZombieSheep 的回答不正确。

My answer to this duplicate question is here:How to tell if a .NET application was compiled in DEBUG or RELEASE mode?

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

Be very careful - just looking at the 'assembly attributes' in the Assembly Manifest for the presence of the 'Debuggable' attribute does NOTmean that you have an assembly that is not JIT optimized. The assembly could be JIT optimized but have the Assembly Output under Advanced Build settings set to include 'full' or 'pdb-only' info - in which case the 'Debuggable' attribute will be present.

要非常小心-只是在“程序集属性”在大会清单为“可调试”属性的存在确实意味着你有一个组件不JIT优化。程序集可以进行 JIT 优化,但将高级构建设置下的程序集输出设置为包含“完整”或“仅 pdb”信息 - 在这种情况下,将出现“可调试”属性。

Please refer to my posts below for more info: How to Tell if an Assembly is Debug or Releaseand How to identify if the DLL is Debug or Release build (in .NET)

有关更多信息,请参阅我下面的帖子: 如何判断程序集是调试还是发布以及 如何识别 DLL 是调试还是发布构建(在 .NET 中)

Jeff Key's application doesn't work correctly, because it identifies a "Debug" build based on if the DebuggableAttribute is present. The DebuggableAttribute is present if you compile in Release mode and choose DebugOutput to anything other than "none".

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

You also need to define exacltywhat is meant by "Debug" vs. "Release"...

您还需要定义exaclty什么是“调试”与“放”的意思?

  • Do you mean that the application is configured with code optimization?
  • Do you mean that you can attach the Visual Studio/JIT Debugger to it?
  • Do you mean that it generates DebugOutput?
  • Do you mean that it defines the DEBUG constant? Remember that you can conditionally compile methods with the System.Diagnostics.Conditional()attribute.
  • 你的意思是应用程序配置了代码优化?
  • 您的意思是可以将 Visual Studio/JIT Debugger 附加到它吗?
  • 你的意思是它生成DebugOutput?
  • 你的意思是它定义了 DEBUG 常量?请记住,您可以有条件地编译具有该System.Diagnostics.Conditional()属性的方法。

回答by Joe Basirico

You're on the right path actually. If you look in the Disassembler window in reflector you will see the following line if it was built in debug mode:

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

[assembly: Debuggable(...)]

回答by flipdoubt

How about using Jeff Key's IsDebugutility? It is a little dated, but since you have Reflector you can decompile it and recompile it in any version of the framework. I did.

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

回答by Max

Here is the VB.Net version of the solution proposed by ZombieSheep

下面是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

Update
This solution works for me but, as Dave Black pointed out, there may be situation where a different approach is needed.
So maybe you can also take a look a Dave Black's answer:

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