c# – 如何确定程序集的构建方式
发布时间:2020-12-15 08:41:51  所属栏目:百科  来源:网络整理 
            导读:我正在使用VS2010 / 2012,我想知道是否有办法(可能使用反射)来查看程序集的构建方式. 当我在Debug中运行时,我使用#if DEBUG将调试信息写入控制台. 但是,当你最终得到一堆程序集时,有没有办法看看它们是如何构建的?获取版本号很简单,但我无法找到如何检查构
                
                
                
            | 我正在使用VS2010 / 2012,我想知道是否有办法(可能使用反射)来查看程序集的构建方式. 
  
  当我在Debug中运行时,我使用#if DEBUG将调试信息写入控制台. 但是,当你最终得到一堆程序集时,有没有办法看看它们是如何构建的?获取版本号很简单,但我无法找到如何检查构建类型. 解决方法
 有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资源管理器来阅读它 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
