简体   繁体   English

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

[英]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.

相关问题 DEBUG vs RELEASE和分发大会 - DEBUG vs RELEASE and distributing Assembly 在 Visual Studio .NET 中调试动态加载的程序集 - Debug dynamically loaded assembly in Visual Studio .NET 如何使用Debug或Release配置检查程序集是否已构建? - How to check if an assembly was built using Debug or Release configuration? 作为 Debug Web API 发布的 Release 程序集的性质是什么 - What is the nature of a Release assembly that is published as a Debug web API 为什么我无法调试动态加载的程序集? - Why am I unable to debug a dynamically loaded assembly? 在运行时加载的程序集中没有Generic.List的调试可视化工具 - No debug visualizer for Generic.List in runtime loaded assembly 如何调试通过Assembly.Load(byte [])加载的程序集? - How do you debug an assembly loaded through Assembly.Load(byte[])? 引用非托管第三方程序集的C#项目在Debug中构建良好,但在Release配置中失败 - The C# project referencing unmanaged third-party assembly builds fine in Debug, but fails in Release configuration 运行T4模板以在解决方案中包含程序集时,确定解决方案配置(调试/发布) - Determine solution configuration (debug/release) when running a T4 template to include assembly in the solution 动态加载的装配体的装配体属性 - Assembly Attributes with Dynamically Loaded Assembly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM