Detecting a dot net assembly’s build configuration

I came across a problem wherein we needed to ensure that when we release our project assemblies they are in “Release” mode and not in “Debug” mode. Google and MSDN gave me two ways:

1. Use the Debuggable Attributes to detect if an assembly is Debug mode or not; as shown in code below:

Assembly assembly = Assembly.LoadFile(fileName);

object[] attributes = assembly.GetCustomAttributes(typeof(DebuggableAttribute), false);

if (attributes.Length > 0)

{

foreach (Attribute attribute in attributes)

{

if (attribute is DebuggableAttribute)

{

DebuggableAttribute dAttribute = attribute as DebuggableAttribute;

if (dAttribute.IsJITOptimizerDisabled)

{ return BuildType.Debug; }

else

{ return BuildType.Release; }

}

}

return BuildType.Other;

}

else

{ return BuildType.Release; }

2. Add information in the AssemblyInfo.cs file and read that at runtime. You can add the following lines to the AssemblyInfo.cs file:

#if DEBUG

[assembly: AssemblyConfiguration("Debug")]

#else

[assembly: AssemblyConfiguration("Release")]

#endif

and then use the following code to read it:

Assembly assembly = Assembly.LoadFile(fileName);

AssemblyConfigurationAttribute config = (AssemblyConfigurationAttribute)AssemblyConfigurationAttribute.GetCustomAttribute(assembly, typeof(AssemblyConfigurationAttribute));

if (config.Configuration == “Debug”)

{ return BuildType.Debug; }

else

{ return BuildType.Release; }

[tag]Dot Net[/tag], [tag]Coding[/tag]



You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply