加载的程序集是 DEBUG 还是 RELEASE?

Posted

技术标签:

【中文标题】加载的程序集是 DEBUG 还是 RELEASE?【英文标题】:Is the loaded assembly DEBUG or RELEASE? 【发布时间】:2014-05-27 05:52:48 【问题描述】:

如何确定加载的程序集是 DEBUG 还是 RELEASE 版本?

是的,我可以使用这样的方法:

public static bool IsDebugVersion() 
#if DEBUG
    return true;
#else
    return false;
#endif

但这只能在我自己的代码中使用。 我需要在运行时进行检查(对于第三方程序集),如下所示:

public static bool IsDebugVersion(Assembly assembly) 
    ???

【问题讨论】:

【参考方案1】:

使用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;

以上摘自here。

使用 LINQ 的替代方案:

public static bool IsAssemblyDebugBuild(Assembly assembly)

    return assembly.GetCustomAttributes(false)
        .OfType<DebuggableAttribute>()
        .Any(i => i.IsJITTrackingEnabled);

【讨论】:

这怎么可能是正确的? docs.microsoft.com/en-us/dotnet/api/… 说Starting with the .NET Framework 2.0, JIT tracking information is always enabled during debugging, and this property value is ignored. @JohnZabroski 出于内部调试器进程的目的而忽略该属性,但对于确定程序集是否在调试模式下编译仍然有效。

以上是关于加载的程序集是 DEBUG 还是 RELEASE?的主要内容,如果未能解决你的问题,请参考以下文章