如何确定特定的 .Net 程序集是否已被 ngen 生成?我需要从代码中检查。即使调用命令行也可以。目前我看不出有任何方法可以确定这一点。
从代码检查
检查我们是否正在为执行程序集加载本机映像。我正在加载的模块文件名属性中寻找模式“\ assemblyname.ni”。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;
namespace MyTestsApp
{
class Program
{
static bool Main(string[] args)
{
Process process = Process.GetCurrentProcess();
ProcessModule[] modules = new ProcessModule[process.Modules.Count];
process.Modules.CopyTo(modules,0);
var niQuery = from m in modules where m.FileName.Contains("\\"+process.ProcessName+".ni") select m.FileName;
bool ni = niQuery.Count()>0 ?true:false;
if (ni)
{
Console.WriteLine("Native Image: "+niQuery.ElementAt(0));
}
else
{
Console.WriteLine("IL Image: " + process.MainModule.FileName);
}
return ni;
}
}
}
命令行解决方案:
在命令提示符下运行“ngen display”。
Example:
ngen 显示 MyTestsApp.exe
如果安装了,它会打印出类似的内容原生图像:
MyTestsApp,版本=1.0.0.0,文化=中性,PublicKeyToken=null
并返回 0 (%errorlevel%)
否则,它打印出:
错误:未安装指定的程序集。
并返回-1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)