How to determine if assembly has been ngen'd?

痴心易碎 提交于 2019-12-03 13:40:06
Sasha

You can try to find your assembly in "ngen cache" (C:\Windows\assembly\NativeImages_v2XXXXXXX).

Сached assemblies will have the following format name: [basename].ni.[baseextension].

Check From Code

Check if we are loading an native image for the executing assembly. I am looking for the pattern "\assemblyname.ni" in loaded module filename property.

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;
        }
    }
}

Command Line Solution:

Run "ngen display " on command prompt.

Example:

ngen display MyTestsApp.exe

If installed, it prints out something like Native Images: MyTestsApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

and returns 0 (%errorlevel%)

Otherwise, it prints out:

Error: The specified assembly is not installed.

and returns -1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!