博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

判断Assembly编译模式的小工具

Posted on 2008-03-18 21:12  ppchen(陈荣林)  阅读(664)  评论(0编辑  收藏  举报
    Assembly发布时可以使用Debug或Release方式,关于Debug和Release的比较园子里已经有很多了,毫无疑问,Debug版本包含Debug信息,只是在开发的时候带来调试的方便,编译器并不对其中的代码进行优化处理,而Release版本无论是在效率或是稳定性上都要高于Debug版本。所以在部署时最好是要确认一下是否使用了Release版本。

    AssemblyCheckTool是一个用于读取检查.NET程序或程序集编译状态的小工具,其原理是通过反射读取Assembly信息,并判断DebuggableAttribute属性值来完成的,核心部分代码如下:
/// <summary>
/// 判断是否为Debug模式
/// Author:ppchen
/// </summary>
private static bool IsAssemblyDebugBuild(string filepath)
{
    
return IsAssemblyDebugBuild(Assembly.LoadFile(Path.GetFullPath(filepath)));
}

private static bool IsAssemblyDebugBuild(Assembly asm)
{
    
object[] objs = asm.GetCustomAttributes(typeof(System.Diagnostics.DebuggableAttribute), true);
    
if (objs.Length > 0)
    {
        DebuggableAttribute debugAtt 
= objs[0as DebuggableAttribute;
        
if (debugAtt != null)
        {
            
return debugAtt.IsJITTrackingEnabled;
        }
    }
    
return false;
}

上一个程序的截图:


程序下载:
AssemblyCheckTool