Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine how an assembly was built

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.

like image 382
Verakso Avatar asked Mar 04 '13 10:03

Verakso


2 Answers

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

like image 113
giammin Avatar answered Oct 11 '22 15:10

giammin


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
like image 36
Jean Hominal Avatar answered Oct 11 '22 15:10

Jean Hominal