如何识别 DLL 是调试版本还是发布版本(在 .NET 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/798971/
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
How to identify if the DLL is Debug or Release build (in .NET)
提问by dr. evil
Possible Duplicate:
How to tell if a .NET application was compiled in DEBUG or RELEASE mode?
I'm sure this has been asked before, but google and SO search failed me.
我确定以前有人问过这个问题,但是 google 和 SO 搜索让我失败了。
How can I identify if a DLL is a release build or debug build?
如何确定 DLL 是发布版本还是调试版本?
采纳答案by this. __curious_geek
The only best way to do this is to check the compiled assemblies itself. There is this very useful tool called '.NET Assembly Information' found hereby Rotem Bloom. After you install this, it associates itself with .dll files to open with itself. After installing you can just double-click on the Assembly to open and it will give you the assembly details as displayed in the screenshots below. There you can identify if it's debug compiled or not.
唯一最好的方法是检查编译的程序集本身。Rotem Bloom在此处找到了一个非常有用的工具,称为“.NET 程序集信息” 。安装后,它会将自身与 .dll 文件关联起来以自行打开。安装后,您只需双击程序集即可打开,它将为您提供程序集详细信息,如下面的屏幕截图所示。在那里你可以确定它是否被调试编译。
Hope this helps..
希望这可以帮助..
回答by Dave Black
IMHO, The above application is really misleading; it only looks for the IsJITTrackingEnabled which is completely independent of whether or not the code is compiled for optimization and JIT Optimization.
恕我直言,上述应用程序确实具有误导性;它只查找 IsJITTrackingEnabled,它完全独立于代码是否为优化和 JIT 优化而编译。
The DebuggableAttribute is present if you compile in Release mode and choose DebugOutput to anything other than "none".
如果您在发布模式下编译并将 DebugOutput 选择为“无”以外的任何内容,则存在 DebuggableAttribute。
You also need to define exactlywhat is meant by "Debug" vs. "Release"...
您还需要准确定义“调试”与“发布”的含义......
Do you mean that the app is configured with code optimization? Do you mean that you can attach the VS/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.
你的意思是应用配置了代码优化?你的意思是你可以将 VS/JIT Debugger 附加到它吗?你的意思是它生成DebugOutput?你的意思是它定义了 DEBUG 常量?请记住,您可以使用 System.Diagnostics.Conditional() 属性有条件地编译方法。
IMHO, when someone asks whether or not an assembly is "Debug" or "Release", they really mean if the code is optimized...
恕我直言,当有人问一个程序集是“调试”还是“发布”时,他们的意思是代码是否经过优化......
Sooo, do you want to do this manually or programmatically?
Sooo,您想手动还是以编程方式执行此操作?
Manually: You need to view the value of the DebuggableAttribute bitmask for the assembly's metadata. Here's how to do it:
手动:您需要查看程序集元数据的 DebuggableAttribute 位掩码的值。这是如何做到的:
- Open the assembly in ILDASM
- Open the Manifest
- Look at the DebuggableAttribute bitmask. If the DebuggableAttribute is not present, it is definitely an Optimized assembly.
- If it is present, look at the 4th byte - if it is a '0' it is JIT Optimized - anything else, it is not:
- 在 ILDASM 中打开装配
- 打开清单
- 查看 DebuggableAttribute 位掩码。如果 DebuggableAttribute 不存在,则它肯定是优化的程序集。
- 如果它存在,请查看第 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 0000 00 00 00 )
// 元数据版本:v4.0.30319 .... // .custom 实例 void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 02 0000 00 00 00 )
Programmatically: assuming that you want to know programmatically if the code is JITOptimized, here is the correct implementation:
以编程方式:假设您想以编程方式知道代码是否经过 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";
}
I've provided this implementation on my blog at:
我在我的博客上提供了这个实现:

