[英]Is the loaded assembly DEBUG or RELEASE?
How can I find out if a loaded assembly is a DEBUG or RELEASE version? 如何确定加载的程序集是DEBUG还是RELEASE版本?
Yes, I could use a method like this: 是的,我可以使用如下方法:
public static bool IsDebugVersion() {
#if DEBUG
return true;
#else
return false;
#endif
}
But this is only usable in my own code. 但这仅在我自己的代码中可用。 I need a check at runtime (for third-party assemblies), like this:
我需要在运行时进行检查(对于第三方程序集),如下所示:
public static bool IsDebugVersion(Assembly assembly) {
???
}
Use Assembly.GetCustomAttributes(bool)
to get a list of attributes, then look for DebuggableAttribute
, and then if that was found, see if the property IsJITTrackingEnabled
is set to true
: 使用
Assembly.GetCustomAttributes(bool)
来获取属性列表,然后查找DebuggableAttribute
,然后找到它,看看属性IsJITTrackingEnabled
是否设置为true
:
public static bool IsAssemblyDebugBuild(Assembly assembly)
{
foreach (var attribute in assembly.GetCustomAttributes(false))
{
var debuggableAttribute = attribute as DebuggableAttribute;
if(debuggableAttribute != null)
{
return debuggableAttribute.IsJITTrackingEnabled;
}
}
return false;
}
The above taken from here . 以上取自这里 。
Alternative using LINQ: 使用LINQ的替代方法:
public static bool IsAssemblyDebugBuild(Assembly assembly)
{
return assembly.GetCustomAttributes(false)
.OfType<DebuggableAttribute>()
.Any(i => i.IsJITTrackingEnabled);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.