.NET Framework 概述Overview of the .NET Framework

.NET Framework 是一种技术,该技术支持生成和运行下一代应用和 XML Web Services。The .NET Framework is a technology that supports building and running the next generation of apps and XML Web services. .NET Framework 旨在实现下列目标:The .NET Framework is designed to fulfill the following objectives:

  • 提供一个一致的面向对象的编程环境,而无论对象代码是在本地存储和执行,还是在本地执行但在 Internet 上分布,或者是在远程执行的。To provide a consistent object-oriented programming environment whether object code is stored and executed locally, executed locally but Internet-distributed, or executed remotely.

  • 提供一个将软件部署和版本控制冲突最小化的代码执行环境。To provide a code-execution environment that minimizes software deployment and versioning conflicts.

  • 提供一个可提高代码(包括由未知的或不完全受信任的第三方创建的代码)执行安全性的代码执行环境。To provide a code-execution environment that promotes safe execution of code, including code created by an unknown or semi-trusted third party.

  • 提供一个可消除脚本环境或解释环境的性能问题的代码执行环境。To provide a code-execution environment that eliminates the performance problems of scripted or interpreted environments.

  • 使开发人员的经验在面对类型大不相同的应用(如基于 Windows 的应用和基于 Web 的应用)时保持一致。To make the developer experience consistent across widely varying types of apps, such as Windows-based apps and Web-based apps.

  • 按照工业标准生成所有通信,确保基于 .NET Framework 的代码可与任何其他代码集成。To build all communication on industry standards to ensure that code based on the .NET Framework integrates with any other code.

备注

有关适用于用户和开发人员的 .NET Framework 的常规说明,请参阅入门For a general introduction to the .NET Framework for both users and developers, see Getting Started.

.NET Framework 包括公共语言运行时 (CLR) 和 .NET Framework 类库。The .NET Framework consists of the common language runtime (CLR) and the .NET Framework class library. 公共语言运行时是 .NET Framework 的基础。The common language runtime is the foundation of the .NET Framework. 可将运行时看作一个在执行时管理代码的代理,它提供内存管理、线程管理和远程处理等核心服务,并且还强制实施严格的类型安全以及可提高安全性和可靠性的其他形式的代码准确性。Think of the runtime as an agent that manages code at execution time, providing core services such as memory management, thread management, and remoting, while also enforcing strict type safety and other forms of code accuracy that promote security and robustness. 事实上,代码管理的概念是运行时的基本原则。In fact, the concept of code management is a fundamental principle of the runtime. 以运行时为目标的代码称为托管代码,而不以运行时为目标的代码称为非托管代码。Code that targets the runtime is known as managed code, while code that doesn't target the runtime is known as unmanaged code. 类库是一个综合性的面向对象的可重用类型集合,可使用它来开发多种应用,这些应用程序包括传统的命令行或图形用户界面 (GUI) 应用,还包括基于 ASP.NET 提供的最新创新的应用(如 Web 窗体和 XML Web Services)。The class library is a comprehensive, object-oriented collection of reusable types that you use to develop apps ranging from traditional command-line or graphical user interface (GUI) apps to apps based on the latest innovations provided by ASP.NET, such as Web Forms and XML Web services.

.NET Framework 可由非托管组件承载,这些组件将公共语言运行时加载到它们的进程中并启动托管代码的执行,从而创建一个同时利用托管和非托管功能的软件环境。The .NET Framework can be hosted by unmanaged components that load the common language runtime into their processes and initiate the execution of managed code, thereby creating a software environment that exploits both managed and unmanaged features. .NET Framework 不但提供若干个运行时主机,而且还支持第三方运行时主机的开发。The .NET Framework not only provides several runtime hosts but also supports the development of third-party runtime hosts.

例如,ASP.NET 承载运行时以为托管代码提供可伸缩的服务器端环境。For example, ASP.NET hosts the runtime to provide a scalable, server-side environment for managed code. ASP.NET 直接使用运行时以启用 ASP.NET 应用和 XML Web Services(本主题稍后将对这两者进行讨论)。ASP.NET works directly with the runtime to enable ASP.NET apps and XML Web services, both of which are discussed later in this topic.

Internet Explorer 是承载运行时(以 MIME 类型扩展的形式)的非托管应用的一个示例。Internet Explorer is an example of an unmanaged app that hosts the runtime (in the form of a MIME type extension). 使用 Internet Explorer 承载运行时使您能够在 HTML 文档中嵌入托管组件或 Windows 窗体控件。Using Internet Explorer to host the runtime enables you to embed managed components or Windows Forms controls in HTML documents. 以这种方式承载运行时可使托管移动代码成为可能,不过它需要进行只有托管代码才能提供的重大改进(如不完全受信任的执行和独立的文件存储)。Hosting the runtime in this way makes managed mobile code possible, but with significant improvements that only managed code offers, such as semi-trusted execution and isolated file storage.

下面的插图显示公共语言运行时和类库与应用之间以及与整个系统之间的关系。The following illustration shows the relationship of the common language runtime and the class library to your apps and to the overall system. 该插图还显示托管代码如何在更大的结构内运行。The illustration also shows how managed code operates within a larger architecture.

显示托管代码如何在大型体系结构中运行的屏幕截图。

以下各节更详细地描述 .NET Framework 的主要功能。The following sections describe the main features of the .NET Framework in greater detail.

公共语言运行时的功能Features of the common language runtime

公共语言运行时管理内存、线程执行、代码执行、代码安全验证、编译以及其他系统服务。The common language runtime manages memory, thread execution, code execution, code safety verification, compilation, and other system services. 这些功能是在公共语言运行时上运行的托管代码所固有的。These features are intrinsic to the managed code that runs on the common language runtime.

至于安全性,取决于包括托管组件的来源(如 Internet、企业网络或本地计算机)在内的一些因素,托管组件被赋予不同程度的信任。Regarding security, managed components are awarded varying degrees of trust, depending on a number of factors that include their origin (such as the Internet, enterprise network, or local computer). 这意味着即使用在同一活动应用中,托管组件既可能能够执行文件访问操作、注册表访问操作或其他须小心使用的功能,也可能不能够执行这些功能。This means that a managed component might or might not be able to perform file-access operations, registry-access operations, or other sensitive functions, even if it's used in the same active app.

运行时还通过实现称为常规类型系统 (CTS) 的严格类型验证和代码验证基础结构来加强代码可靠性。The runtime also enforces code robustness by implementing a strict type-and-code-verification infrastructure called the common type system (CTS). CTS 确保所有托管代码都是可以自我描述的。The CTS ensures that all managed code is self-describing. 各种 Microsoft 编译器和第三方语言编译器都可生成符合 CTS 的托管代码。The various Microsoft and third-party language compilers generate managed code that conforms to the CTS. 这意味着托管代码可在严格实施类型保真和类型安全的同时使用其他托管类型和实例。This means that managed code can consume other managed types and instances, while strictly enforcing type fidelity and type safety.

此外,运行时的托管环境还消除了许多常见的软件问题。In addition, the managed environment of the runtime eliminates many common software issues. 例如,运行时自动处理对象布局并管理对对象的引用,在不再使用它们时将它们释放。For example, the runtime automatically handles object layout and manages references to objects, releasing them when they are no longer being used. 这种自动内存管理解决了两个最常见的应用错误:内存泄漏和无效内存引用。This automatic memory management resolves the two most common app errors, memory leaks and invalid memory references.

运行时还提高了开发人员的工作效率。The runtime also accelerates developer productivity. 例如,程序员用他们选择的开发语言编写应用,却仍能充分利用其他开发人员用其他语言编写的运行时、类库和组件。For example, programmers write apps in their development language of choice yet take full advantage of the runtime, the class library, and components written in other languages by other developers. 任何选择以运行时为目标的编译器供应商都可以这样做。Any compiler vendor who chooses to target the runtime can do so. 以 .NET Framework 为目标的语言编译器使得用该语言编写的现有代码可以使用 .NET Framework 的功能,这大大减轻了现有应用的迁移过程的工作负担。Language compilers that target the .NET Framework make the features of the .NET Framework available to existing code written in that language, greatly easing the migration process for existing apps.

尽管运行时是为未来的软件设计的,但是它也支持现在和以前的软件。While the runtime is designed for the software of the future, it also supports software of today and yesterday. 托管和非托管代码之间的互操作性使开发人员能够继续使用所需的 COM 组件和 DLL。Interoperability between managed and unmanaged code enables developers to continue to use necessary COM components and DLLs.

运行时旨在增强性能。The runtime is designed to enhance performance. 尽管公共语言运行时提供许多标准运行时服务,但是它从不解释托管代码。Although the common language runtime provides many standard runtime services, managed code is never interpreted. 一种称为实时 (JIT) 编译的功能使所有托管代码能够以它在其上执行的系统的本机语言运行。A feature called just-in-time (JIT) compiling enables all managed code to run in the native machine language of the system on which it's executing. 同时,内存管理器排除了出现零碎内存的可能性,并增大了内存引用区域以进一步提高性能。Meanwhile, the memory manager removes the possibilities of fragmented memory and increases memory locality-of-reference to further increase performance.

最后,运行时可由高性能的服务器端应用(如 Microsoft SQL Server 和 Internet Information Services (IIS))承载。Finally, the runtime can be hosted by high-performance, server-side apps, such as Microsoft SQL Server and Internet Information Services (IIS). 此基础结构使您在享受支持运行时承载的行业最佳企业服务器的优越性能的同时,能够使用托管代码编写业务逻辑。This infrastructure enables you to use managed code to write your business logic, while still enjoying the superior performance of the industry's best enterprise servers that support runtime hosting.

.NET Framework Class Library — .NET Framework 类库.NET Framework class library

.NET Framework 类库是一个与公共语言运行时紧密集成的可重用的类型集合。The .NET Framework class library is a collection of reusable types that tightly integrate with the common language runtime. 该类库是面向对象的,并提供某些类型,可供你自己的托管代码从中派生功能。The class library is object oriented, providing types from which your own managed code derives functionality. 这不但使 .NET Framework 类型易于使用,而且还减少了学习 .NET Framework 的新功能所需要的时间。This not only makes the .NET Framework types easy to use but also reduces the time associated with learning new features of the .NET Framework. 此外,第三方组件与 .NET Framework 中的类无缝集成。In addition, third-party components integrate seamlessly with classes in the .NET Framework.

例如,.NET Framework 集合类实现一组用于开发自己的集合类的接口。For example, the .NET Framework collection classes implement a set of interfaces for developing your own collection classes. 你的集合类与 .NET Framework 中的类无缝地混合。Your collection classes blend seamlessly with the classes in the .NET Framework.

正如您对面向对象的类库所希望的那样,.NET Framework 类型使您能够完成一系列常见编程任务(包括诸如字符串管理、数据收集、数据库连接以及文件访问等任务)。As you would expect from an object-oriented class library, the .NET Framework types enable you to accomplish a range of common programming tasks, including tasks such as string management, data collection, database connectivity, and file access. 除这些常规任务之外,类库还包括支持多种专用开发方案的类型。In addition to these common tasks, the class library includes types that support a variety of specialized development scenarios. 使用 .NET Framework 开发下列类型的应用和服务:Use the .NET Framework to develop the following types of apps and services:

Windows 窗体类是一组综合性的可重用的类型,它们大大简化了 Windows GUI 的开发。The Windows Forms classes are a comprehensive set of reusable types that vastly simplify Windows GUI development. 如果要编写 ASP.NET Web 窗体应用,可使用 Web 窗体类。If you write an ASP.NET Web Form app, you can use the Web Forms classes.

请参阅See also