Reputation: 37378
I have a referenced library, inside there I want to perform a different action if the assembly that references it is in DEBUG/RELEASE mode.
Is it possible to switch on the condition that the calling assembly is in DEBUG/RELEASE mode?
Is there a way to do this without resorting to something like:
bool debug = false;
#if DEBUG
debug = true;
#endif
referencedlib.someclass.debug = debug;
The referencing assembly will always be the starting point of the application (i.e. web application.
Upvotes: 9
Views: 1205
Reputation: 2736
The accepted answer is correct. Here's an alternative version that skips the iteration stage and is provided as an extension method:
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;
}
}
Upvotes: 2
Reputation: 49609
Google says it is very simple. You get the information from the DebuggableAttribute of the assembly in question:
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;
}
Upvotes: 11
Reputation: 8053
You can use reflection to get the calling assembly and use this method to check if it is in debug mode.
Upvotes: 0