.net - 如何识别 DLL 是 Debug 还是 Release 构建(在 .NET 中)

标签 .net dll build debugging release

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

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

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

最佳答案

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

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

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

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

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

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

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

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

// Metadata version: v4.0.30319 .... // .custom instance void [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

关于.net - 如何识别 DLL 是 Debug 还是 Release 构建(在 .NET 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/798971/

相关文章:

android - Multiple Errors Unity (SDK APP Building) (on Mac) 信息见图片

python - 在 Mac 上生成等效的 python "jar"以在 Windows 上运行

.net - 在 .NET 中手动实现高性能算法

c# - Silverlight 的轻量级分析?

c# - 作为管道的 RX IObservable

dll - 用dmd编译D2语言时如何从DLL中导出变量?

c# - 一个 DLL,具有两个应用程序的两个实现

c# - 如何在 2 个不同的 DLL 中访问具有相同完全限定名称的类型

kotlin - Gradle Kotlin DSL扩展自

.Net,XmlDSigEx : CngKey. 使用来自 X509Certificate2 的公钥导入