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

dr.*_*vil 102 .net debugging dll release build

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

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

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

thi*_*eek 92

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

希望这可以帮助..

  • 事实证明,它是我们开发团队中非常出色的工具.太奇妙了! (4认同)
  • 没有为win 10工作,未能加载"Microsoft.Owin.*" (3认同)
  • 很好找.谢谢你的链接. (2认同)
  • @user908645 可以在 https://github.com/tebjan/AssemblyInformation 上找到更新的分支。我使用了 .msi 安装程序。在我的 Win10 机器上工作正常。 (2认同)

Dav*_*ack 92

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

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

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

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

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

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

手动:您需要查看程序集元数据的DebuggableAttribute位掩码的值.这是怎么做的:

  1. 在ILDASM中打开程序集
  2. 打开清单
  3. 查看DebuggableAttribute位掩码.如果DebuggableAttribute不存在,它肯定是优化程序集.
  4. 如果存在,请查看第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)

以编程方式:假设您想以编程方式了解代码是否是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";
}
Run Code Online (Sandbox Code Playgroud)

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

如何判断程序集是调试还是发布

  • http://assemblyinformation.codeplex.com/上的工具已更新,您可能希望修改您的答案 (2认同)
  • 我还要指出,既然您知道自己在寻找什么(感谢 Black 先生),JetBrains 的 Dot Peek 等工具将为您提供这些信息。使用 Dot Peek,您可以在程序集资源管理器中双击程序集本身(不是程序集下的任何文件),它将显示他正在使用他的工具检查的所有程序集属性。关键的两个是 Debuggable 属性和 IsJITOptimizerDisabled 缺失。 (2认同)
  • @CaitLANJenner - 这是我写的一个工具的片段,它比这更好.我正在考虑放置Git.无论如何,在上面的代码中,将"ReflectedAssembly"替换为您要检查的Assembly的实例.你可以使用Assembly.LoadFrom(...)或旧的(不推荐的)用法 - Assembly.Load(..),Assembly.LoadFile(..)或Assembly.LoadWithPartialName(..)来实现这一点. (2认同)

归档时间:

查看次数:

67865 次

最近记录:

7 年,9 月 前