或许在开发过程中,你会遇到这种情况,我们拿到一个dll或者exe,不知道这个程序集是debug还是release版本。其实C#开发中,我们是在JIT运行环境中来判断程序集,是否是debug还是release版本。直接上代码吧,如下是控制台程序,比较简单:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(DebugBuild("D:\\Test.exe")); //这里就是要验证的程序集路径
}
private static bool DebugBuild(string path)
{
return DebugBuild(Assembly.LoadFile(Path.GetFullPath(path)));
}
private static bool DebugBuild(Assembly assembly)
{
foreach (object attribute in assembly.GetCustomAttributes(false))
{
if (attribute is DebuggableAttribute)
{
DebuggableAttribute _attribute = attribute as DebuggableAttribute;
return _attribute.IsJITTrackingEnabled;
}
}
return false;
}
}
}
另外有关JIT知识补充点,有的时候,我们会遇到JIT运行时出错,解决方式参考上上篇文章:http://blog.youkuaiyun.com/u014650759/article/details/78042043