6

I'm using VS2010/2012 and I was wondering if there is a way (perhaps using reflection) to see how an assembly is build.

When I run in Debug, I use the #if DEBUG to write debug information out to the console.

However, when you end up with a bunch of assemblies, is there then a way to see how they where build? Getting the version number is easy, but I haven't been able to find out how to check the build type.

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
Verakso
  • 466
  • 5
  • 16

2 Answers2

3

Once they are compiled, you can't, unless you put the metadata yourself.

For example, you could use either AssemblyConfigurationAttribute or .NET 4.5's AssemblyMetadataAttribute

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif

or

#if DEBUG
[assembly: AssemblyMetadata("DefinedVariable", "DEBUG")]
#endif
Jean Hominal
  • 16,518
  • 5
  • 56
  • 90
3

There are 3 ways:

private bool IsAssemblyDebugBuild(string filepath)
{
    return IsAssemblyDebugBuild(Assembly.LoadFile(Path.GetFullPath(filepath)));
}

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;
}

Or using assemblyinfo metadata:

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif

Or using a constant with #if DEBUG in code

#if DEBUG
        public const bool IsDebug = true;
#else
        public const bool IsDebug = false;
#endif

I prefer the second way so i can read it both by code and with windows explorer

giammin
  • 18,620
  • 8
  • 71
  • 89
  • I did se you answer, but I also read the original post, and blog post, and ended up with at slightly modified version of `IsAssemblyDebugBuild`that looked like this: `return assembly.GetCustomAttributes(false).Any(x => (x as DebuggableAttribute) != null ? (x as DebuggableAttribute).IsJITTrackingEnabled : false);` – Verakso Mar 04 '13 at 13:14