使用 .NET Native 编译引用Compiling Apps with .NET Native

.NET Native 是一种用于生成和部署 Visual Studio 2015 及更高版本附带的 Windows 应用的预编译技术。.NET Native is a precompilation technology for building and deploying Windows apps that is included with Visual Studio 2015 and later versions. 它自动将以托管代码(C# 或 Visual Basic)编写并面向 .NET Framework 和 Windows 10 的发布版本的应用编译为本机代码。It automatically compiles the release version of apps that are written in managed code (C# or Visual Basic) and that target the .NET Framework and Windows 10 to native code.

通常,定位于 .NET Framework 的应用会被编译到中间语言 (IL) 中。Typically, apps that target the .NET Framework are compiled to intermediate language (IL). 在运行时间,及时生成 (JIT) 编译器将 IL 翻译为本机代码。At run time, the just-in-time (JIT) compiler translates the IL to native code. 与此相反,.NET Native 会直接将 Windows 应用编译为本机代码。In contrast, .NET Native compiles Windows apps directly to native code. 对于开发者,这意味着:For developers, this means:

  • 您的应用程序具有本机代码的性能。Your apps feature the performance of native code. 通常,性能比首次编译为 IL 的代码更优越,并由 JIT 编译器编译为本机代码。Usually, performance will be superior to code that is first compiled to IL and then compiled to native code by the JIT compiler.

  • 你可以继续用 C# 或 Visual Basic 进行编程。You can continue to program in C# or Visual Basic.

  • 你可以继续使用 .NET Framework 提供的资源,包括类库、自动内存管理、垃圾回收和异常处理。You can continue to take advantage of the resources provided by the .NET Framework, including its class library, automatic memory management and garbage collection, and exception handling.

对于你的应用程序的用户,.NET Native 具有以下优势:For users of your apps, .NET Native offers these advantages:

  • 大多数应用和方案的执行时间更快。Faster execution times for the majority of apps and scenarios.

  • 大多数应用和方案的启动时间更快。Faster startup times for the majority of apps and scenarios.

  • 低部署和更新成本。Low deployment and update costs.

  • 优化的应用内存使用情况。Optimized app memory usage.

重要

对于绝大多数应用和方案,与编译到 IL 或 NGEN 映像的应用相比,.NET Native 提供明显更快的启动时间和更高的性能。For the vast majority of apps and scenarios, .NET Native offers significantly faster startup times and superior performance when compared to an app compiled to IL or to an NGEN image. 但是,您的结果可能会有所不同。However, your results may vary. 若要确保你的应用程序已受益于 .NET Native 的性能增强,你应将其性能与应用程序的 non-.NET 本机版本的性能进行比较。To ensure that your app has benefited from the performance enhancements of .NET Native, you should compare its performance with that of the non-.NET Native version of your app. 有关详细信息,请参阅 性能会话概述For more information, see Performance Session Overview.

但 .NET Native 只涉及到本机代码的编译。But .NET Native involves more than a compilation to native code. 它会改变 .NET Framework 应用的创建和执行方式。It transforms the way that .NET Framework apps are built and executed. 具体而言:In particular:

  • 在预编译期间,所需的 .NET Framework 部分会静态连接到你的应用。During precompilation, required portions of the .NET Framework are statically linked into your app. 这允许应用同 .NET Framework 的应用本地库一起运行,并且允许编译器执行全局分析,从而提供性能优势。This allows the app to run with app-local libraries of the .NET Framework, and the compiler to perform global analysis to deliver performance wins. 因此,应用甚至在 .NET Framework 更新后会启动得更快。As a result, apps launch consistently faster even after .NET Framework updates.

  • .NET Native 运行时针对静态预编译进行了优化,在大多数情况下,可提供优异的性能。The .NET Native runtime is optimized for static precompilation and in the vast majority of cases offers superior performance. 同时,它保留了开发者认为非常高效的核心反射特性。At the same time, it retains the core reflection features that developers find so productive.

  • .NET Native 使用的后端与 c + + 编译器相同,后者针对静态预编译方案进行了优化。.NET Native uses the same back end as the C++ compiler, which is optimized for static precompilation scenarios.

.NET Native 可以将 c + + 的性能优势引入到托管代码开发人员,因为它使用的是与 c + + 中的 c + + 相同或类似的工具,如下表所示。.NET Native is able to bring the performance benefits of C++ to managed code developers because it uses the same or similar tools as C++ under the hood, as shown in this table.

.NET Native.NET Native C++C++
Libraries .NET Framework + Windows 运行时The .NET Framework + Windows Runtime Win32 + Windows 运行时Win32 + Windows Runtime
编译器Compiler UTC 优化编译器UTC optimizing compiler UTC 优化编译器UTC optimizing compiler
已部署Deployed 随时可以运行的二进制代码Ready-to-run binaries 随时可以运行的二进制代码 (ASM)Ready-to-run binaries (ASM)
运行时Runtime MRT.dll(最短 CLR 运行时)MRT.dll (Minimal CLR Runtime) CRT.dll(C 运行时)CRT.dll (C Runtime)

对于适用于 Windows 10 的 Windows 应用,可将应用程序包(.appx 文件)中的 .NET 本机代码编译二进制文件上载到 Windows 应用商店。For Windows apps for Windows 10, you upload .NET Native Code Compilation binaries in app packages (.appx files) to the Windows Store.

本节内容In This Section

要查看有关带有 .NET Native 代码编译的应用开发的信息,请参阅以下主题:For more information about developing apps with .NET Native Code Compilation, see these topics: