joe
joe

Reputation: 8634

Is the loaded assembly DEBUG or RELEASE?

How can I find out if a loaded assembly is a DEBUG or RELEASE version?

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) {
    ???
}

Upvotes: 0

Views: 342

Answers (1)

Haney
Haney

Reputation: 34742

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:

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:

public static bool IsAssemblyDebugBuild(Assembly assembly)
{
    return assembly.GetCustomAttributes(false)
        .OfType<DebuggableAttribute>()
        .Any(i => i.IsJITTrackingEnabled);
}

Upvotes: 3

Related Questions