.NET 中的程序集Assemblies in .NET

程序集构成了 .NET 应用程序的部署、版本控制、重用、激活范围和安全权限的基本单元。Assemblies form the fundamental units of deployment, version control, reuse, activation scoping, and security permissions for .NET-based applications. 程序集是为协同工作而生成的类型和资源的集合,这些类型和资源构成了一个逻辑功能单元。An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. 程序集采用可执行文件 (.exe) 或动态链接库文件 (.dll) 的形式,是 .NET 应用程序的构建基块 。Assemblies take the form of executable (.exe) or dynamic link library (.dll) files, and are the building blocks of .NET applications. 它们向公共语言运行时提供了注意类型实现代码所需的信息。They provide the common language runtime with the information it needs to be aware of type implementations. 可以将程序集视为一组构成功能逻辑单元并旨在配合使用的类型和资源。You can think of an assembly as a collection of types and resources that form a logical unit of functionality and are built to work together.

在 .NET Core 和 .NET Framework 中,可以从一个或多个源代码文件生成程序集。In .NET Core and .NET Framework, you can build an assembly from one or more source code files. 在 .NET Framework 中,程序集可以包含一个或多个模块。In .NET Framework, assemblies can contain one or more modules. 因此,大型项目可以采用以下规划:由多个开发者单独开发各源代码文件或模块,最后整合所有这些内容以创建一个程序集。This allows larger projects to be planned so that several developers can work on separate source code files or modules, which are combined to create a single assembly. 若要详细了解模块,请参阅操作说明:生成多文件程序集For more information about modules, see How to: Build a multifile assembly.

程序集具有以下属性:Assemblies have the following properties:

  • 程序集以 .exe 或 .dll 文件的形式实现 。Assemblies are implemented as .exe or .dll files.

  • 对于面向 .NET Framework 的库,可以通过将程序集放入全局程序集缓存 (GAC),在应用程序之间共享程序集。For libraries that target the .NET Framework, you can share assemblies between applications by putting them in the global assembly cache (GAC). 必须先对程序集进行强命名,然后才能将它们包含到 GAC 中。You must strong-name assemblies before you can include them in the GAC. 有关详细信息,请参阅具有强名称的程序集For more information, see Strong-named assemblies.

  • 只有在需要使用时才会将程序集加载到内存中。Assemblies are only loaded into memory if they are required. 如果未使用程序集,则不加载。If they aren't used, they aren't loaded. 也就是说,使用程序集,可以在大型项目中高效管理资源。This means that assemblies can be an efficient way to manage resources in larger projects.

  • 可以使用反射,以编程方式获取程序集的相关信息。You can programmatically obtain information about an assembly by using reflection. 有关详细信息,请参阅反射 (C#)反射 (Visual Basic)For more information, see Reflection (C#) or Reflection (Visual Basic).

  • 你可以加载一个程序集,以使用 .NET Core 中的 MetadataLoadContext 类以及 .NET Core 和 .NET Framework 中的 Assembly.ReflectionOnlyLoadAssembly.ReflectionOnlyLoadFrom 方法来检查该程序集。You can load an assembly just to inspect it by using the MetadataLoadContext class in .NET Core and the Assembly.ReflectionOnlyLoad or Assembly.ReflectionOnlyLoadFrom method in .NET Core and .NET Framework.

公共语言运行时中的程序集Assemblies in the common language runtime

程序集向公共语言运行时提供了注意类型实现代码所需的信息。Assemblies provide the common language runtime with the information it needs to be aware of type implementations. 对于运行时,类型不存在于程序集上下文之外。To the runtime, a type does not exist outside the context of an assembly.

程序集定义以下信息:An assembly defines the following information:

  • 公共语言运行时执行的代码。Code that the common language runtime executes. 请注意,每个程序集只能有一个入口点:DllMainWinMainMainNote that each assembly can have only one entry point: DllMain, WinMain, or Main.

  • 安全边界。Security boundary. 程序集就是在其中请求和授予权限的单元。An assembly is the unit at which permissions are requested and granted. 有关程序集中安全边界的详细信息,请参阅程序集安全注意事项For more information about security boundaries in assemblies, see Assembly security considerations.

  • 类型边界。Type boundary. 每一类型的标识均包括该类型所驻留的程序集的名称。Every type's identity includes the name of the assembly in which it resides. 在一个程序集的范围中加载的称为 MyType 的类型不同于在另一个程序集范围中加载的称为 MyType 的类型。A type called MyType that is loaded in the scope of one assembly is not the same as a type called MyType that is loaded in the scope of another assembly.

  • 引用范围边界。Reference scope boundary. 程序集清单包含用于解析类型和满足资源请求的元数据。The assembly manifest has metadata that is used for resolving types and satisfying resource requests. 该清单指定要在程序集外公开的类型和资源,并枚举它所依赖的其他程序集。The manifest specifies the types and resources to expose outside the assembly, and enumerates other assemblies on which it depends. 除非可迁移可执行 (PE) 文件中的 Microsoft 中间语言 (MSIL) 代码具有相关的程序集清单,否则不执行此代码。Microsoft intermediate language (MSIL) code in a portable executable (PE) file won't be executed unless it has an associated assembly manifest.

  • 版本边界。Version boundary. 程序集是公共语言运行时中无版本冲突的最小单元。The assembly is the smallest versionable unit in the common language runtime. 同一程序集中的所有类型和资源均会被版本化为一个单元。All types and resources in the same assembly are versioned as a unit. 程序集清单描述你为任何依赖项程序集所指定的版本依赖性。The assembly manifest describes the version dependencies you specify for any dependent assemblies. 有关版本控制的详细信息,请参阅程序集版本控制For more information about versioning, see Assembly versioning.

  • 部署单元。Deployment unit. 当一个应用程序启动时,只有该应用程序最初调用的程序集必须存在。When an application starts, only the assemblies that the application initially calls must be present. 其他程序集(例如,包含本地化资源或实用工具类的程序集)可以按需检索。Other assemblies, such as assemblies containing localization resources or utility classes, can be retrieved on demand. 这样,应用在第一次下载时就会比较精简。This allows apps to be simple and thin when first downloaded. 有关部署程序集的详细信息,请参阅部署应用程序For more information about deploying assemblies, see Deploy applications.

  • 并行执行单元。Side-by-side execution unit. 有关运行多个版本的程序集的详细信息,请参阅程序集和并行执行For more information about running multiple versions of an assembly, see Assemblies and side-by-side execution.

创建程序集Create an assembly

程序集可以为静态或动态。Assemblies can be static or dynamic. 静态程序集存储在磁盘上的可迁移可执行 (PE) 文件中。Static assemblies are stored on disk in portable executable (PE) files. 静态程序集可以包括接口、类和资源(如位图、JPEG 文件和其他资源文件)。Static assemblies can include interfaces, classes, and resources like bitmaps, JPEG files, and other resource files. 你还可以创建动态程序集,动态程序集直接从内存运行并且在执行前不保存到磁盘上。You can also create dynamic assemblies, which are run directly from memory and aren't saved to disk before execution. 你可以在执行动态程序集后将它们保存在磁盘上。You can save dynamic assemblies to disk after they have executed.

有几种创建程序集的方法。There are several ways to create assemblies. 你可以使用可创建 .dll 或 .exe 文件的开发工具,例如 Visual Studio 。You can use development tools, such as Visual Studio, that can create .dll or .exe files. 可以使用 Windows SDK 中的工具创建具有从其他开发环境中创建的模块的程序集。You can use tools in the Windows SDK to create assemblies with modules from other development environments. 还可以使用公共语言运行时 API(例如 System.Reflection.Emit)来创建动态程序集。You can also use common language runtime APIs, such as System.Reflection.Emit, to create dynamic assemblies.

可以采用以下方法编译程序集:在 Visual Studio 中生成程序集、使用 .NET Core 命令行接口工具生成程序集,或使用命令行编译器生成 .NET Framework 程序集。Compile assemblies by building them in Visual Studio, building them with .NET Core command-line interface tools, or building .NET Framework assemblies with a command-line compiler. 有关使用 .NET Core 命令行接口工具生成程序集的详细信息,请参阅 .NET Core 命令行接口工具For more information about building assemblies using .NET Core command-line interface tools, see .NET Core command-line interface tools. 要了解如何使用命令行编译器生成程序集,请参阅使用 csc.exe 的命令行生成(适用于 C#),或者从命令行生成(适用于 Visual Basic)。For building assemblies with the command-line compilers, see Command-line build with csc.exe for C#, or Build from the command line for Visual Basic.

备注

若要在 Visual Studio 中生成程序集,请在“生成”菜单上选择“生成” 。To build an assembly in Visual Studio, on the Build menu, select Build.

程序集清单Assembly manifest

每个程序集都有一个程序集清单文件 。Every assembly has an assembly manifest file. 与目录类似,程序集清单包含以下内容:Similar to a table of contents, the assembly manifest contains:

  • 程序集的标识(名称和版本)。The assembly's identity (its name and version).

  • 文件表,描述构成程序集的其他所有文件(例如,.exe 或 .dll 文件所依赖的你创建的其他程序集、位图文件或自述文件) 。A file table describing all the other files that make up the assembly, such as other assemblies you created that your .exe or .dll file relies on, bitmap files, or Readme files.

  • 程序集引用列表,即所有外部依赖项的列表,如 .dll 或其他文件 。An assembly reference list, which is a list of all external dependencies, such as .dlls or other files. 程序集既可以引用全局对象,也可以引用私有对象。Assembly references contain references to both global and private objects. 全局对象可用于所有其他应用程序。Global objects are available to all other applications. 在 .NET Core 中,全局对象与特定的 .NET Core 运行时结合使用。In .NET Core, global objects are coupled with a particular .NET Core runtime. 在 .NET Framework 中,全局对象位于全局程序集缓存 (GAC) 中。In .NET Framework, global objects reside in the global assembly cache (GAC). System.IO.dll 是 GAC 中程序集的一个示例 。System.IO.dll is an example of an assembly in the GAC. 私有对象必须位于级别不高于应用安装目录的目录中。Private objects must be in a directory level at or below the directory in which your app is installed.

由于程序集包含内容、版本控制和依赖项的相关信息,因此使用它们的应用程序不依赖 Windows 系统上的注册表等外部源也能正常运行。Because assemblies contain information about content, versioning, and dependencies, the applications that use them needn't rely on external sources, such as the registry on Windows systems, to function properly. 程序集减少了 .dll 冲突,让应用程序变得更可靠、更易于部署 。Assemblies reduce .dll conflicts and make your applications more reliable and easier to deploy. 在许多情况下,只需将 .NET 应用程序的文件复制到目标计算机,即可进行安装。In many cases, you can install a .NET-based application simply by copying its files to the target computer. 有关详细信息,请参阅程序集清单For more information, see Assembly manifest.

添加对程序集的引用Add a reference to an assembly

必须添加对应用程序中的程序集的引用,才能使用该程序集。To use an assembly in an application, you must add a reference to it. 引用程序集后,应用程序可以使用其名称空间的所有可访问类型、属性、方法和其他成员,就好像它们的代码是源文件的一部分一样。Once an assembly is referenced, all the accessible types, properties, methods, and other members of its namespaces are available to your application as if their code were part of your source file.

备注

.NET 类库中的大多数程序集都是自动引用的。Most assemblies from the .NET Class Library are referenced automatically. 如果系统程序集不是自动引用的,则对于 .NET Core,可以添加对包含该程序集的 NuGet 包的引用。If a system assembly isn't automatically referenced, for .NET Core, you can add a reference to the NuGet package that contains the assembly. 请使用 Visual Studio 中的 NuGet 包管理器,或者将程序集的 <PackageReference> 元素添加到 .csproj 或 .vbproj 项目 。Either use the NuGet Package Manager in Visual Studio, or add a <PackageReference> element for the assembly to the .csproj or .vbproj project. 在 .NET Framework 中,可以通过在 Visual Studio 中使用“添加引用”对话框,或者通过使用 C#Visual Basic 编译器的 -reference 命令行选项,添加对该程序集的引用 。In .NET Framework, you can add a reference to the assembly by using the Add Reference dialog in Visual Studio, or by using the -reference command line option for the C# or Visual Basic compilers.

在 C# 中,可以在单个应用程序中使用同一程序集的两个版本。In C#, you can use two versions of the same assembly in a single application. 有关详细信息,请参阅外部别名For more information, see extern alias.

TitleTitle 说明Description
程序集内容Assembly contents 组成程序集的元素。Elements that make up an assembly.
程序集清单Assembly manifest 程序集清单中的数据,以及它如何存储在程序集中。Data in the assembly manifest, and how it is stored in assemblies.
全局程序集缓存Global assembly cache GAC 如何存储和使用程序集。How the GAC stores and uses assemblies.
具有强名称的程序集Strong-named assemblies 具有强名称的程序集的特征。Characteristics of strong-named assemblies.
程序集安全注意事项Assembly security considerations 安全性如何作用于程序集。How security works with assemblies.
程序集版本控制Assembly versioning .NET Framework 版本控制策略的概述。Overview of the .NET Framework versioning policy.
程序集位置Assembly placement 在何处可以找到程序集。Where to locate assemblies.
程序集和并行执行Assemblies and side-by-side execution 同时使用多个版本的运行时或程序集。Use multiple versions of the runtime or an assembly simultaneously.
使用程序集编程Program with assemblies 如何在程序集上创建、签署和设置特性。How to create, sign, and set attributes on assemblies.
发出动态方法和程序集Emit dynamic methods and assemblies 如何创建动态程序集。How to create dynamic assemblies.
运行时如何定位程序集How the runtime locates assemblies .NET Framework 如何在运行时解析程序集引用。How the .NET Framework resolves assembly references at run time.

参考Reference

System.Reflection.Assembly

请参阅See also