可能的重复:
如何判断 .NET 应用程序是在 DEBUG 还是 RELEASE 模式下编译的?
我确信以前有人问过这个问题,但是谷歌和SO搜索让我失败了。
如何识别 DLL 是发布版本还是调试版本?
恕我直言,上述应用程序确实具有误导性;它只查找 IsJITTrackingEnabled,它完全独立于代码是否经过编译以进行优化和 JIT 优化。
如果您在发布模式下编译并选择 DebugOutput 为“none”以外的任何值,则 DebuggableAttribute 会出现。
您还需要定义exactly“调试”与“发布”是什么意思?
您的意思是该应用程序配置了代码优化吗?
您的意思是可以将 VS/JIT 调试器附加到它吗?
你的意思是它生成 DebugOutput 吗?
您的意思是它定义了 DEBUG 常量吗?请记住,您可以使用 System.Diagnostics.Conditional() 属性有条件地编译方法。
恕我直言,当有人问程序集是“调试”还是“发布”时,他们真正的意思是代码是否经过优化......
那么,您想手动还是以编程方式执行此操作?
Manually:
您需要查看程序集元数据的 DebuggableAttribute 位掩码的值。操作方法如下:
- 在 ILDASM 中打开装配体
- 打开清单
- 查看 DebuggableAttribute 位掩码。如果 DebuggableAttribute 不存在,则它肯定是一个优化的程序集。
- 如果存在,请查看第 4 个字节 - 如果它是“0”,则它是 JIT 优化的 - 其他任何字节都不是:
// 元数据版本:v4.0.30319 .... // .custom 实例 void
[mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype
[mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = (
01 00 020000 00 00 00 )
以编程方式:假设您想以编程方式了解代码是否经过 JITOptimized,以下是正确的实现(在简单的控制台应用程序中):
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}");
}
我在我的博客上提供了这个实现:
如何判断程序集是调试还是发布
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)