本文介绍了如何使用 ngen.exe 进行编译以及如何运行生成的本机代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出于特殊目的使用 ngen 命令行编译 C# 程序.所以我在 VS2010 中创建了一个控制台应用程序并将其命名为 ngentest.在 vs2010projectsgentestindebug 中创建名为 ngentest.vshost.exe 的文件.我在VS2010命令提示符中使用了这个文件作为ngen命令参数,如下:

I want to compile a C# program using ngen command line for a special purpose. So I create a console application in VS2010 and named it ngentest. A file by name ngentest.vshost.exe is created in vs2010projectsgentestindebug.I used this file as a ngen command argument in VS2010 command prompt, as follows:

ngen "c:documentsvs2010projects
gentestindebug
gentest.vshost.exe"

但是当我这样做时,我无法接收PublicKeyToken,而且我在任何地方都找不到任何程序集!如果我的程序集已创建,它在哪里?我怎么能找到它?我如何运行它(使用命令,或...!)来获取我的输出?

But when I do this, I can't receive PublicKeyToken and I couldn't find any assembly anywhere!If my assembly is created, where it is? And how I can find it? How I can run it(with command, or...!) to get my output?

否则,当我从 VS 的 Build 菜单中使用 build ngen 构建我的项目时,在提到的目录中创建了一些文件,其中之一是 ngentest.exe.

Otherwise when I build my project with build ngen from Build menu from VS, some file were created in mentioned directory, and one of them is ngentest.exe.

推荐答案

当您编译 C# 代码时,它会被编译为一个 IL 程序集.NGEN 以 IL 程序集作为输入,并将程序集及其依赖项安装到 Native Image Cache 中.

When you compile your C# code, it gets compiled into an IL assembly. And NGEN takes IL assembly as an input and installs the assembly and its dependencies into Native Image Cache.

对于您的示例二进制文件,您需要打开一个管理员 VS 命令提示符,然后键入以下内容

For your example binary, you need to open an admin VS Command prompt, then type the following

ngen install ngentest.exe

这会将您的 exe 及其依赖项 dll 文件安装到本机图像缓存中.您在此处使用程序集的文件名.

This would install your exe and its dependency dll files into the Native Image Cache. You use the filename to the assembly here.

然后,当您运行 exe 时,.NET 运行时将加载并运行安装到本机映像缓存的本机映像.您无需采取任何额外步骤即可让 .NET 运行本机映像.运行时检查本机映像缓存以查看是否存在用于 IL 程序集的有效本机映像.

Then when you run your exe, the .NET runtime will load and run the native image installed to the Native Image Cache. You don't need to take any extra step to have .NET run the Native Image. The runtime checks the Native Image Cache to see if there is a valid Native Image for the IL assembly.

您可以通过键入以下命令来验证是否安装了本机映像:

You can verify that a native image is installed by typing the following command:

ngen display ngentest

在这种情况下,您必须使用程序集名称.请注意,32 位 ngen 将仅安装和显示 32 位程序集,而 64 位 ngen 将仅安装和显示 64 位程序集.

In this case you must use the assembly name. Note that 32 bit ngen will only install and display 32 bit, assemblies and 64 bit ngen only 64 bitassemblies.

参见 http://blogs.msdn.com/b/junfeng/archive/2007/02/18/native-image-loading.aspx 了解更多关于原生图片加载的信息.

See http://blogs.msdn.com/b/junfeng/archive/2007/02/18/native-image-loading.aspx for more info on Native Image loading.

请注意,ngentest.vhost.exe 是 VS 为提供更好的调试体验而创建的工件.它由 VS 使用.您不应该将它用于 NGEN 或任何与此相关的事情.看问题:vshost.exe文件的用途是什么? 了解更多信息.

Note that ngentest.vhost.exe is an artifact created by VS to provide better debugging experience. It is used by VS. You shouldn't be using that for NGEN or anything for that matter. See the question: What is the purpose of the vshost.exe file? for more info.

这篇关于如何使用 ngen.exe 进行编译以及如何运行生成的本机代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 17:16