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 位掩码的值。操作方法如下:
- 在 ILDASM 中打开程序集
- 打开 list
- 查看 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 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}");
}
我在我的博客上提供了这个实现:
关于.net - 如何识别 DLL 是 Debug 还是 Release 构建(在 .NET 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/798971/