如何确定程序集的构建方式

时间:2013-03-04 10:31:31

标签: c# visual-studio-2010 build visual-studio-2012 .net-assembly

我正在使用VS2010 / 2012,我想知道是否有办法(可能使用反射)来查看程序集的构建方式。

当我在Debug中运行时,我使用#if DEBUG将调试信息写入控制台。

然而,当你最终得到一堆程序集时,有没有办法看看它们在哪里构建?获取版本号很简单,但我无法找到如何检查构建类型。

2 个答案:

答案 0 :(得分:3)

编译完成后,除非自己输入元数据,否则不能。

例如,您可以使用AssemblyConfigurationAttribute或.NET 4.5的AssemblyMetadataAttribute

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

#if DEBUG
[assembly: AssemblyMetadata("DefinedVariable", "DEBUG")]
#endif

答案 1 :(得分:3)

有三种方式:

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

或使用assemblyinfo元数据:

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

或在代码中使用#if DEBUG的常量

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

我更喜欢第二种方式,所以我可以通过代码和Windows资源管理器来阅读它