首页 文章

如何识别DLL是否是Debug或Release版本(在.NET中)[重复]

提问于
浏览
90

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

我确定之前已经问过这个问题,但google和SO搜索失败了 .

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

2 回答

  • 77

    恕我直言,上述申请真的具有误导性;它只查找IsJITTrackingEnabled,它完全独立于代码是否为优化和JIT优化而编译 .

    如果在Release模式下编译并选择DebugOutput为“none”以外的任何值,则存在DebuggableAttribute .

    你还需要准确定义"Debug"与"Release"的含义......

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

    恕我直言,当有人询问程序集是否为“Debug”或“Release”时,它们的确意味着代码是否已经优化...

    Sooo,你想手动或以编程方式执行此操作吗?

    Manually :您需要查看程序集's metadata. Here'的DebuggableAttribute位掩码的值:

    • 在ILDASM中打开程序集

    • 打开清单

    • 查看DebuggableAttribute位掩码 . 如果DebuggableAttribute不存在,它肯定是优化程序集 .

    • 如果它存在,请查看第4个字节 - 如果它是'0'它是JIT优化 - 其他任何东西,它不是:

    //元数据版本: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)

    Programmatically :假设您想以编程方式了解代码是否是JITOptimized,这是正确的实现:

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

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

    How to Tell if an Assembly is Debug or Release

  • 88

    唯一的最佳方法是检查已编译的程序集本身 . 有一个非常有用的工具叫做'.NET Assembly Information'由Rotem Bloom找到here . 安装它之后,它将自己与.dll文件关联以自行打开 . 安装完成后,只需双击程序集即可打开,它将为您提供下面屏幕截图中显示的程序集详细信息 . 在那里,您可以识别是否已编译调试 .

    希望这可以帮助..

相关问题