How to check if an assembly was built using Debug or Release configuration?

后端 未结 6 408
半阙折子戏
半阙折子戏 2020-12-13 04:16

I\'m starting deployment of my web application and I need to guarantee that all the assemblies that are going to be deployed were built using Release configuration. Our syst

6条回答
  •  执念已碎
    2020-12-13 05:06

    If it is your assembly I believe using the AssemblyConfiguration attribute is the best approach. It is documented as "Specifies the build configuration, such as retail or debug, for an assembly."

    Depending on your build configurations you might have code like this:

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

    Then check the assembly attribute:

    public static bool IsAssemblyConfiguration(Assembly assembly, string configuration)
    {
        var attributes = assembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false);
        if (attributes.Length == 1)
        {
            var assemblyConfiguration = attributes[0] as AssemblyConfigurationAttribute;
            if (assemblyConfiguration != null)
            {
                return assemblyConfiguration.Configuration.Equals(configuration, StringComparison.InvariantCultureIgnoreCase);
            }
        }
        return true;
    }
    

    (I know R. Schreurs comment at Rubens Farias says the same, but I've find this information somewhere else before seeing the comment so I believe this requires a more important entry like a full response instead of a comment)

提交回复
热议问题