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";
}
您应该检查程序集(或 .net dll 或程序集)的属性
解释:
DebuggableAttribute.IsJITTrackingEnabled
是诀窍,因为如果已生成调试信息,则为真。注意:当您从编译器运行项目时,它可能不起作用。尝试从命令行运行它。
然后你会像这样使用它:
我从这里的英文站点获取了 IsAssemblyDebugBuild 函数。
版:
在这个存储库中,我给你留下了一个有效的例子。在调试和发布模式下运行它以查看它的外观。
https://github.com/rnrneverdies/IsAssemblyDebugBuild.git
离开:
正如您在评论中所说,有一个选项:
DebuggableAttribute.IsJITOptimizerDisabled
但请注意,如果删除优化,它可能会在 Release 中给出误报。最简单最快的方法是使用ILDASM
要运行它,只需打开 Visual Studio 开发控制台并运行 ILDASM 命令
然后打开你的.exe或.dll,找到任何方法,双击它,尝试
nop
在反编译的代码中找到指令如果您
nop
在其他指令中找到指令,则意味着它是在 mode 下编译的,DEBUG
如果您没有找到它在 mode 中的任何指令RELEASE
。例子:
对于以下代码:
当我们得到这个时打开
.exe
编译好的:DEBUG
在模式下
RELEASE
,我们得到以下信息:注意
nop
前后Console::WriteLine
一篇关于这个主题的优秀文章。
如何判断程序集是调试还是发布
据我了解,在编译时,我们必须从中获取数据
DebuggableAttribute
,然后才能知道IsJITOptimizerDisabled
指示代码是否优化的值最重要的线是
指示编译是否已优化
当你使用这条线时
这将
ReflectedAssembly
是我们要评估的程序集,您可以使用Assembly.GetExecutingAssembly()
它或加载其他一些外部库来评估