[英]Alternate method in referenced assembly when in debug mode using [Conditional(“DEBUG”)]
[英]How to tell if calling assembly is in DEBUG mode from compiled referenced lib
我有一个引用的库,在那里,如果引用它的程序集处于DEBUG / RELEASE模式,我想执行不同的操作。
是否可以打开调用程序集处于DEBUG / RELEASE模式的条件?
有没有办法做到这一点,而不诉诸于:
bool debug = false;
#if DEBUG
debug = true;
#endif
referencedlib.someclass.debug = debug;
引用程序集始终是应用程序的起点(即Web应用程序。
谷歌说这很简单。 您从相关程序集的DebuggableAttribute获取信息:
IsAssemblyDebugBuild(Assembly.GetCallingAssembly());
private bool IsAssemblyDebugBuild(Assembly assembly)
{
foreach (var attribute in assembly.GetCustomAttributes(false))
{
var debuggableAttribute = attribute as DebuggableAttribute;
if(debuggableAttribute != null)
{
return debuggableAttribute.IsJITTrackingEnabled;
}
}
return false;
}
接受的答案是正确的。 这是一个跳过迭代阶段的替代版本,作为扩展方法提供:
public static class AssemblyExtensions
{
public static bool IsDebugBuild(this Assembly assembly)
{
if (assembly == null)
{
throw new ArgumentNullException(nameof(assembly));
}
return assembly.GetCustomAttribute<DebuggableAttribute>()?.IsJITTrackingEnabled ?? false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.