【问题标题】:How to identify if the DLL is Debug or Release build (in .NET) [duplicate]如何识别 DLL 是 Debug 还是 Release 构建(在 .NET 中)[重复]
【发布时间】:2010-10-22 09:19:20
【问题描述】:

可能重复:
How to tell if a .NET application was compiled in DEBUG or RELEASE mode?

我确定以前有人问过这个问题,但谷歌和 SO 搜索失败了。

如何识别 DLL 是发布版本还是调试版本?

【问题讨论】:

标签: .net dll build debugging release


【解决方案1】:

执行此操作的唯一最佳方法是检查已编译的程序集本身。 Rotem Bloom 在here 发现了一个非常有用的工具,称为“.NET 程序集信息”。安装后,它会将自身与 .dll 文件关联以自行打开。安装后,您只需双击程序集即可打开,它将为您提供程序集详细信息,如下面的屏幕截图所示。在那里你可以确定它是否正在调试 编译与否。

希望这会有所帮助..

【讨论】:

  • 不错的发现。感谢您的链接。
  • 在我们的开发团队中,它已被证明是一个非常出色的工具。太棒了!
  • 支持.net框架
  • 不适用于 win 10,无法加载“Microsoft.Owin.*”
  • @user908645 可以在github.com/tebjan/AssemblyInformation 找到更新的分支。我使用了 .msi 安装程序。在我的 Win10 机器上运行良好。
【解决方案2】:

恕我直言,上述应用程序确实具有误导性;它只查找完全独立于代码是否为优化和 JIT 优化而编译的 IsJITTrackingEnabled。

如果您在 Release 模式下编译并选择 DebugOutput 为“none”以外的任何内容,则会出现 DebuggableAttribute。

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

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

恕我直言,当有人问一个程序集是“调试”还是“发布”时,他们的真正意思是代码是否经过优化......

Sooo,您要手动还是以编程方式执行此操作?

手动: 您需要查看程序集元数据的 DebuggableAttribute 位掩码的值。操作方法如下:

  1. 在 ILDASM 中打开程序集
  2. 打开清单
  3. 查看 DebuggableAttribute 位掩码。如果 DebuggableAttribute 不存在,它肯定是一个优化程序集。
  4. 如果存在,请查看第 4 个字节 - 如果为“0”,则为 JIT 优化 - 其他任何内容都不是:

// 元数据版本:v4.0.30319 .... // .custom 实例无效 [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 02 00 00 00 00 00 )

以编程方式:假设您想以编程方式了解代码是否经过 JIT 优化,以下是正确的实现(在简单的控制台应用中):

void Main()
{
    var HasDebuggableAttribute = false;
    var IsJITOptimized = false;
    var IsJITTrackingEnabled = false;
    var BuildType = "";
    var DebugOutput = "";
    
    var ReflectedAssembly = Assembly.LoadFile(@"path to the dll you are testing");
    object[] attribs = ReflectedAssembly.GetCustomAttributes(typeof(DebuggableAttribute), false);

    // If the 'DebuggableAttribute' is not found then it is definitely an OPTIMIZED build
    if (attribs.Length > 0)
    {
        // Just because the 'DebuggableAttribute' is found doesn't necessarily mean
        // it's a DEBUG build; we have to check the JIT Optimization flag
        // i.e. it could have the "generate PDB" checked but have JIT Optimization enabled
        DebuggableAttribute debuggableAttribute = attribs[0] as DebuggableAttribute;
        if (debuggableAttribute != null)
        {
            HasDebuggableAttribute = true;
            IsJITOptimized = !debuggableAttribute.IsJITOptimizerDisabled;
            
            // IsJITTrackingEnabled - Gets a value that indicates whether the runtime will track information during code generation for the debugger.
            IsJITTrackingEnabled = debuggableAttribute.IsJITTrackingEnabled;
            BuildType = debuggableAttribute.IsJITOptimizerDisabled ? "Debug" : "Release";

            // check for Debug Output "full" or "pdb-only"
            DebugOutput = (debuggableAttribute.DebuggingFlags &
                            DebuggableAttribute.DebuggingModes.Default) !=
                            DebuggableAttribute.DebuggingModes.None
                            ? "Full" : "pdb-only";
        }
    }
    else
    {
        IsJITOptimized = true;
        BuildType = "Release";
    }

    Console.WriteLine($"{nameof(HasDebuggableAttribute)}: {HasDebuggableAttribute}");
    Console.WriteLine($"{nameof(IsJITOptimized)}: {IsJITOptimized}");
    Console.WriteLine($"{nameof(IsJITTrackingEnabled)}: {IsJITTrackingEnabled}");
    Console.WriteLine($"{nameof(BuildType)}: {BuildType}");
    Console.WriteLine($"{nameof(DebugOutput)}: {DebugOutput}");
}

我在我的博客上提供了这个实现:

How to Tell if an Assembly is Debug or Release

【讨论】:

  • assemblyinformation.codeplex.com的工具已经更新,您不妨修改一下答案
  • 我还要指出,现在您知道自己在寻找什么(感谢 Black 先生),JetBrains 的 Dot Peek 等工具可以为您提供这些信息。使用 Dot Peek,您可以在“程序集资源管理器”中双击程序集本身(不是程序集下的任何文件),它将向您显示他正在使用他的工具检查的所有程序集属性。关键的两个是 Debuggable 属性和 IsJITOptimizerDisabled 缺失。
  • 您能解释一下如何在您的 DLL 上使用编程方法吗?我不知道如何让该代码打开/找到我的 DLL,除非我遗漏了一些东西。 object[] 属性如何知道要引用哪个 DLL?
  • @CaitLANJenner - 这是我编写的一个工具的 sn-p,它的作用远不止于此。我正在考虑放在 Git 上。无论如何,在上面的代码中,将“ReflectedAssembly”替换为您要检查的程序集实例。您可以使用 Assembly.LoadFrom(...) 或旧的(已弃用)用法来执行此操作 - Assembly.Load(..)、Assembly.LoadFile(..) 或 Assembly.LoadWithPartialName(..)
  • 在 dotnet core 中不起作用...发布和调试结果相同的输出:HasDebuggableAttribute True; IsJITOptimized False;构建类型调试;调试输出已满
猜你喜欢
  • 2012-06-05
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 2017-05-10
  • 2013-03-21
  • 1970-01-01
  • 2010-09-16
相关资源
最近更新 更多