简体   繁体   English

命名空间是程序集还是项目?

[英]Is a namespace an assembly, or a project?

Just reviewing some .NET stuff for an interview tomorrow, 只是回顾一些.NET的东西明天面试,

MSDN defines the Internal keyword as follows: MSDN定义Internal关键字如下:

Internal types or members are accessible only within files in the same assembly 内部类型或成员只能在同一程序集中的文件中访问

But what I cannot seem to find is the definition for an assembly. 但我似乎无法找到的是装配的定义。 Is that everything within the same namespace, the same project, or...? 这是同一个命名空间,同一个项目或......中的所有内容吗?

Nope inside an assembly you may have define more than one namespaces. 在程序集内部,您可能已经定义了多个名称空间。 An assembly is the smallest unit of deployment in .NET. 程序集是.NET中最小的部署单元。 It is a collection of your IL code and metadata about you code. 它是您的IL代码和有关代码的元数据的集合。 It contains a file called Manifest in which all the types that are used in your assembly or referred to are declared. 它包含一个名为Manifest的文件,其中声明了程序集中使用的所有类型或引用的所有类型。 Also there are some other files. 还有一些其他文件。

As it is stated more formally in MSDN : 正如在MSDN中更正式地说明的那样:

Assemblies are the building blocks of .NET Framework applications; 程序集是.NET Framework应用程序的构建块; they form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions. 它们构成了部署,版本控制,重用,激活范围和安全权限的基本单元。 An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. 程序集是一组类型和资源的集合,这些类型和资源构建为一起工作并形成逻辑功能单元。 An assembly provides 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. 对于运行时,在程序集的上下文之外不存在类型。

On the other hand for a namespace we have that 另一方面,对于namespace我们有

The namespace keyword is used to declare a scope that contains a set of related objects. namespace关键字用于声明包含一组相关对象的范围。 You can use a namespace to organize code elements and to create globally unique types. 您可以使用命名空间来组织代码元素并创建全局唯一类型。

as it is stated in MSDN 正如MSDN中所述

From the above it is clear that you can declare as many namespaces you want inside an assembly. 从上面可以清楚地看到,您可以在程序集中声明所需的名称空间。

A nice and free tool that you can use to see what is in an assembly is the IL Disassembler . IL Disassembler是一个很好的免费工具,可用于查看程序集中的内容。

An Assembly is a Project that has been compiled into a .DLL or .EXE. 程序集是一个已编译为.DLL或.EXE的项目。 For the purposes of Visual Studio, any Class Library project, or Windows Forms App, or WPF App will compile into an Assembly. 出于Visual Studio的目的,任何类库项目,Windows窗体应用程序或WPF应用程序都将编译为程序集。

An assembly is a physical container file (DLL or EXE). 程序集是物理容器文件(DLL或EXE)。

Although somewhat unusual, a namespace can span multiple assemblies. 虽然有些不寻常,但命名空间可以跨越多个程序集

Internal classes are not necessarily visible to all classes within the same namespace, since a namespace can span multiple assemblies. 内部类不一定对同一命名空间中的所有类可见,因为命名空间可以跨越多个程序集。 The assembly is the compiled file (DLL or EXE). 程序集是编译文件(DLL或EXE)。

Usually it means within the same project, but there are methods to combine the output of multiple projects into one assembly (via ILMERGE). 通常它意味着在同一个项目中,但有一些方法可以将多个项目的输出组合成一个程序集(通过ILMERGE)。

Assemblies and namespaces are orthogonal. 程序集和命名空间是正交的。 One assembly may contain multiple namespaces and a single namespace may span multiple assemblies. 一个程序集可能包含多个名称空间,单个名称空间可能跨多个程序集。 Assemblies are the smallest independently versionable unit of code in the .NET framework. 程序集是.NET框架中最小的可独立版本化的代码单元。 Assemblies define the physical organization of your code (into dll or exe files), while namespaces define the logical organization. 程序集定义代码的物理组织(到dll或exe文件),而名称空间定义逻辑组织。 A single assembly is a single dll or exe file. 单个程序集是单个dll或exe文件。 A namespace is logical grouping typically based on technology or function. 命名空间是通常基于技术或功能的逻辑分组。 System.Net deals with networking. System.Net处理网络。 System.Text deals with text manipulation and encodings. System.Text处理文本操作和编码。

Note that I haven't mentioned projects at all. 请注意,我根本没有提到过项目。 A project is something that is only really relevant to a particular build system. 项目只与特定的构建系统真正相关。 The .NET runtime has no knowledge of projects, per se. .NET运行时本身并不了解项目。 Since most people use Visual Studio and MSBuild tools for development and the default is to have a single project output a single assembly, people tend to use the terms project and assembly interchangeably in casual conversation. 由于大多数人使用Visual Studio和MSBuild工具进行开发,并且默认是将单个项目输出为单个程序集,因此人们倾向于在临时对话中交替使用术语项目和程序集。 Other tools like MonoDevelop follow a similar pattern, but there is no rule dictating that a single project must produce a single assembly. MonoDevelop等其他工具遵循类似的模式,但没有规则规定单个项目必须生成单个程序集。

In the .NET world, Assembly is a package of compiled code. 在.NET世界中,Assembly是一个编译代码包。 It can be either an executable( .exe file) or a shared library( .dll file) and can contain multiple namespaces. 它可以是可执行文件( .exe文件),也可以是共享库( .dll文件),并且可以包含多个名称空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM