如何确定程序集是否已生成?

发布于 2024-08-20 08:28:12 字数 76 浏览 3 评论 0原文

如何确定特定的 .Net 程序集是否已被 ngen 生成?我需要从代码中检查。即使调用命令行也可以。目前我看不出有任何方法可以确定这一点。

How can you determine whether a particular .Net assembly has already been ngen'd or not? I need to check from code. Even invoking the command-line would be fine. At the moment I can't see any way of determining this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡忘如思 2024-08-27 08:28:12

从代码检查

检查我们是否正在为执行程序集加载本机映像。我正在加载的模块文件名属性中寻找模式“\ 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”。

示例:

ngen display MyTestsApp.exe

如果已安装,它会打印出类似的内容
原生图像:
MyTestsApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

并返回 0 (%errorlevel%)

否则,它将打印出:

错误:未安装指定的程序集。

返回-1

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

唱一曲作罢 2024-08-27 08:28:12

您可以尝试在“ngen缓存”(C:\ Windows \ assembly \ NativeImages_v2XXXXXXX)中找到您的程序集。

Cached 程序集将具有以下格式名称:
[基本名称].ni.[基本扩展名]

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].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文