C# 如何确定程序集是如何生成的

C# 如何确定程序集是如何生成的,c#,visual-studio-2010,build,visual-studio-2012,.net-assembly,C#,Visual Studio 2010,Build,Visual Studio 2012,.net Assembly,我正在使用VS2010/2012,我想知道是否有一种方法(可能是使用反射)来查看如何构建程序集 在Debug中运行时,我使用#if Debug将调试信息写入控制台 然而,当您最终得到一组程序集时,是否有一种方法可以查看它们是如何构建的?获取版本号很容易,但我还无法找到如何检查生成类型。一旦编译了它们,您就不能,除非您自己放置元数据 例如,您可以使用或.NET 4.5的 或 有三种方法: private bool IsAssemblyDebugBuild(string filepath) {

我正在使用VS2010/2012,我想知道是否有一种方法(可能是使用反射)来查看如何构建程序集

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


然而,当您最终得到一组程序集时,是否有一种方法可以查看它们是如何构建的?获取版本号很容易,但我还无法找到如何检查生成类型。

一旦编译了它们,您就不能,除非您自己放置元数据

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

有三种方法:

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
        public const bool IsDebug = true;
#else
        public const bool IsDebug = false;
#endif

我更喜欢第二种方法,这样我可以通过代码和windows资源管理器阅读它

您所说的“检查构建类型”是什么意思?您真正感兴趣的是什么?(条件编译符号?优化选项?调试选项?)你能澄清一下你想要实现什么吗?完全重复:@Dennis,我以为我已经对此做了彻底的搜索-显然没有,对不起,但你是绝对正确的。我没有回答你,但我也阅读了原始帖子和博客帖子,最后得到了稍微修改过的
IsAssemblyDebugBuild
版本,如下所示:
返回assembly.GetCustomAttributes(false).Any(x=>(x作为DebuggableAttribute)!=null?(x作为DebuggableAttribute.IsJITTrackingEnabled:false)
#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
#if DEBUG
        public const bool IsDebug = true;
#else
        public const bool IsDebug = false;
#endif