【问题标题】:Is the loaded assembly DEBUG or RELEASE?加载的程序集是 DEBUG 还是 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) {
    ???
}

【问题讨论】:

    标签: c# debugging configuration release


    【解决方案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 出于内部调试器进程的目的而忽略该属性,但对于确定程序集是否在调试模式下编译仍然有效。
    猜你喜欢
    • 2013-03-21
    • 2017-05-10
    • 1970-01-01
    • 2015-06-25
    • 2010-09-16
    • 2010-10-22
    • 1970-01-01
    相关资源
    最近更新 更多