简体   繁体   English

命名空间还是程序集?

[英]Namespace or Assembly?

I am getting very confused between Namespaces and Assemblies. 我在命名空间和程序集之间变得非常困惑。 Are System.Data and System.Web Namespaces or Assemblies? System.DataSystem.Web命名空间还是程序集?

I have noticed these are called namespaces and at the same time they are present in GAC_32 folder. 我注意到它们被称为命名空间,同时它们存在于GAC_32文件夹中。 So what exactly are they? 那究竟是什么?

System.Data is a namespace , System.Data.DLL (the file) is an assembly . System.Data是一个名称空间System.Data.DLL (该文件)是一个程序集

A namespace is a logical grouping of types (mostly to avoid name collisions). 命名空间是类型的逻辑分组(主要是为了避免名称冲突)。 An assembly can contain types in multiple namespaces ( System.DLL contains a few...), and a single namespace can be spread across assemblies (eg System.Threading ). 程序集可以包含多个名称空间中的类型( System.DLL包含一些...),并且单个名称空间可以跨程序集分布(例如System.Threading )。

Namespace is a logical grouping of classes belongs to same functionality. 命名空间是属于相同功能的类的逻辑分组。 So System.Web and System.Data are namespaces 所以System.WebSystem.Data是命名空间

MSDN describe it as: MSDN将其描述为:

Namespaces are heavily used in C# programming in two ways. 命名空间在两种方式中大量用于C#编程。 First, the .NET Framework uses namespaces to organize its many classes Secondly, declaring your own namespaces can help control the scope of class and method names in larger programming projects. 首先,.NET Framework使用命名空间来组织其许多类。其次,声明自己的命名空间可以帮助控制较大编程项目中的类和方法名称的范围。

命名空间

Assembly is chunk of (precompiled) code that can be executed by the .NET runtime environment. 程序集是可以由.NET运行时环境执行的(预编译)代码块。 It contains one or more than one Namespaces. 它包含一个或多个命名空间。 A .NET program consists of one or more assemblies. .NET程序由一个或多个程序集组成。

System.Web.dll and System.Data.dll are assemblies. System.Web.dllSystem.Data.dll是程序集。

MSDN describe it as: 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. 对于运行时,在程序集的上下文之外不存在类型。

部件

In short: 简而言之:

Assembly: 部件:

An assembly provides a fundamental unit of physical code grouping.It is an Output Unit. 程序集提供了物理代码分组的基本单元。它是一个输出单元。 It is a unit of Deployment & a unit of versioning. 它是部署单元和版本控制单元。 Assemblies contain MSIL code. 程序集包含MSIL代码。

Namespace: 命名空间:

A namespace provides a fundamental unit of logical code grouping.It is a Collection of names where in each name is Unique.They form the logical boundary for a Group of classes.Namespace must be specified in Project-Properties. 命名空间提供了逻辑代码分组的基本单元。它是一个名称集合,其中每个名称都是唯一的。它们构成了一组类的逻辑边界。必须在Project-Properties中指定名称空间。

They are namespaces.Assemblies contains more than one namespace.For Example: System.dll contains these namespaces (and more): 它们是namespaces.Assemblies包含多个名称空间。例如: System.dll包含这些名称空间(以及更多):

在此输入图像描述

Also one namespace might contain nested namespaces.They are just logical names to organize the code.Just be aware, a DLL files are assemblies that contains namespace(s). 另外一个命名空间可能包含嵌套的命名空间。它们只是组织代码的逻辑名称。请注意, DLL文件是包含命名空间的程序集。

GAC is Global Assembly Cache . GAC全局程序集缓存 According to MSDN: 根据MSDN:

The global assembly cache stores assemblies specifically designated to be shared by several applications on the computer. 全局程序集缓存存储专门指定由计算机上的多个应用程序共享的程序集。

So commonly used assemblies stored in the GAC and therefore you don't need to copy all assembly files to your project directory that you are referencing from your project.The assemblies stored in the GAC are Strong-Named assemblies.Normally when you add a reference to an assembly from your project that is not Strong-Named a copy of your .dll file will be created on your bin\\Debug folder..If you wish you can make your assembly (class library project for example) Strong-Named.See: How to: Sign an Assembly with a Strong Name 存储在GAC中的常用程序集,因此您无需将所有程序集文件复制到您从项目引用的项目目录中。存储在GAC程序集是强名称程序集。通常在添加引用时从您的项目中没有Strong-Named的程序集将在您的bin\\Debug文件夹中创建.dll文件的副本。如果您希望可以进行组装(例如类库项目)Strong-Named.See : 如何:使用强名称签署程序集

In short: 简而言之:

  • Assembly is stored as an .EXE or .DLL files. 程序集存储为.EXE或.DLL文件。
  • Namespace is a way of grouping type names and reducing the chance of name collisions. 命名空间是一种对类型名称进行分组并减少名称冲突的方法。

Tips. 提示。

An assembly contains a collection of types (for example l'assembly System contains many namespaces included System, System.IO, ecc). 程序集包含一组类型(例如l'程序集System包含许多名称空间,包括System,System.IO,ecc)。 Usually, the name of assembly is the same of a namespace that it contains but not always. 通常,程序集的名称与它包含的名称空间相同,但并非总是如此。

Other example of assemblies and namespaces. 程序集和命名空间的其他示例。

Assembly 1 ( CoreAssembly.DLL ) 程序集1( CoreAssembly.DLL

Contains namespaces Namespace1.subnamespace1 包含名称空间Namespace1.subnamespace1

Assembly 2 ( ExtensionCoreAssembly.DLL ) 程序集2( ExtensionCoreAssembly.DLL

Contains namespaces Namespace1.subnamespace1 包含名称空间Namespace1.subnamespace1

It is possible use name of assembly that contains different namespaces and extend an existing assembly with an other assembly by this technique. 可以使用包含不同命名空间的程序集名称,并通过此技术使用其他程序集扩展现有程序集。

DEFINITIONS. 定义。

Assemblies 大会

An assembly is a collection of types and resources that forms a logical unit of functionality. 程序集是一组类型和资源,它们构成了一个逻辑逻辑单元。 All types in the .NET Framework must exist in assemblies; .NET Framework中的所有类型都必须存在于程序集中; the common language runtime does not support types outside of assemblies. 公共语言运行库不支持程序集之外的类型。 Each time you create a Microsoft Windows® Application, Windows Service, Class Library, or other application with Visual Basic .NET, you're building a single assembly. 每次使用Visual Basic .NET创建MicrosoftWindows®应用程序,Windows服务,类库或其他应用程序时,您都在构建单个程序集。 Each assembly is stored as an .exe or .dll file. 每个程序集都存储为.exe或.dll文件。 Note Although it's technically possible to create assemblies that span multiple files, you're not likely to use this technology in most situations. 注意虽然从技术上讲可以创建跨多个文件的程序集,但在大多数情况下,您不太可能使用此技术。

Namespaces 命名空间

Another way to organize your Visual Basic .NET code is through the use of namespaces. 组织Visual Basic .NET代码的另一种方法是使用命名空间。 Namespaces are not a replacement for assemblies, but a second organizational method that complements assemblies. 命名空间不是程序集的替代品,而是补充程序集的第二种组织方法。 Namespaces are a way of grouping type names and reducing the chance of name collisions. 命名空间是一种对类型名称进行分组并减少名称冲突的方法。 A namespace can contain both other namespaces and types. 命名空间可以包含其他命名空间和类型。 The full name of a type includes the combination of namespaces that contain that type. 类型的全名包括包含该类型的命名空间的组合。

Link: http://msdn.microsoft.com/en-us/library/ms973231.aspx 链接: http//msdn.microsoft.com/en-us/library/ms973231.aspx

Others have given very good and detailed answers to this question. 其他人对这个问题给出了非常好的和详细的答案。 But I want to point out that when you're not sure, you can look on MSDN. 但我想指出,当你不确定时,你可以查看MSDN。 The MSDN library explains very clearly and simply the namespace and assembly in which any given type resides. MSDN库非常清楚地解释了任何给定类型所在的命名空间程序集 It even says the name of the file (in System.Data.dll) so there is no ambiguity. 它甚至说文件的名称(in System.Data.dll)所以没有歧义。

在此输入图像描述

The file that you see in GAC is System.Data.dll that is an assembly and that contains namespaces including System.Data . 您在GAC中看到的文件是System.Data.dll ,它是一个程序集,包含名称空间,包括System.Data If you view the Reference properties in Visual studio then you will see: 如果您在Visual Studio中查看Reference属性,那么您将看到:

在此输入图像描述

Later if you right click on the reference and select view in object browser you will see namespaces in that particular assembly. 稍后如果右键单击引用并在对象浏览器中选择视图,您将在该特定程序集中看到名称空间。

在此输入图像描述

As @amdluigi says, "Usually, the name of assembly is the same as a namespace that it contains but not always". 正如@amdluigi所说,“通常,程序集的名称与它包含的名称空间相同,但并非总是如此”。

There is a screenshot above of System.Data.dll in the Object Browser in Studio. Studio中的对象浏览器中有一个System.Data.dll的屏幕截图。 It's an excellent example to explore the issues here. 这是探索这里问题的一个很好的例子。 Note that most of the namespaces contained within the assembly are System.Data or a sub-namespace of System.Data. 请注意,程序集中包含的大多数命名空间都是System.Data或System.Data的子命名空间。

In general, it is a good practice for assembly names to be related to the namespaces within them. 通常,将程序集名称与其中的名称空间相关联是一种很好的做法。 Notice that when Studio first creates a project to build an assembly, one of the project properties is a default namespace. 请注意,当Studio首次创建项目以构建程序集时,其中一个项目属性是默认名称空间。 At the start, Studio gives default namespace the same name as the project itself. 一开始,Studio提供的默认命名空间与项目本身的名称相同。 If you ever choose to rename a project, do consider changing its default namespace as well. 如果您选择重命名项目,请考虑更改其默认命名空间。

There are two extra namespaces: Microsoft.SqlServer is understandable. 还有两个额外的命名空间:Microsoft.SqlServer是可以理解的。 Some SQL Server types that they didn't want to package in a separate assembly. 一些SQL Server类型,他们不想在单独的程序集中打包。

But what's with System.Xml???? 但是System.Xml是什么???? There is a System.Xml.dll assembly. 有一个System.Xml.dll程序集。 Why is this namespace showing up in System.Data.dll as well? 为什么这个命名空间也出现在System.Data.dll中?

Notice that an assembly can reopen a namespace and add more to it - that's exactly what System.Data.dll is doing with the System.Xml namespace. 请注意,程序集可以重新打开命名空间并向其添加更多内容 - 这正是System.Data.dll正在使用System.Xml命名空间执行的操作。

The reason is that namespaces have zero performance implications, but assemblies very much do. 原因是名称空间没有性能影响,但程序集非常重要。 If you have 1000 classes with substantial amounts of code, you don't want one assembly with a very large memory footprint. 如果您有1000个具有大量代码的类,则不希望一个具有非常大的内存占用的程序集。 Neither do you want 1000 assemblies each with one class. 您也不希望每个1000个程序集都有一个类。 Any assembly needs to be loaded into memory before its contents can be executed. 在执行其内容之前,需要将任何程序集加载到内存中。 You want an assembly to contain a reasonable number of interrelated classes, so once your application has loaded an assembly to obtain one of its classes, it gets other classes the application is likely to need for free. 您希望程序集包含合理数量的相互关联的类,因此一旦您的应用程序加载了一个程序集以获取其中一个类,它就会获得应用程序可能需要的其他类。 Granularity is important: not too big, not too small, just right. 粒度很重要:不要太大,不要太小,恰到好处。

Notice that System.Data.dll reopens System.Xml and adds exactly one class: XmlDataDocument. 请注意,System.Data.dll重新打开System.Xml并只添加一个类:XmlDataDocument。 It happens that this class is used to interpret relational data as an XML document. 碰巧该类用于将关系数据解释为XML文档。 If your application is just using XML, it won't need this class. 如果您的应用程序只使用XML,则不需要此类。 If your application deals with relational data, it might. 如果您的应用程序处理关系数据,它可能会。 So while XmlDataDocument inherits from XmlDocument and is in the System.Xml namespace, it's packaged in the System.Data.dll assembly. 因此,虽然XmlDataDocument继承自XmlDocument并且位于System.Xml命名空间中,但它已打包在System.Data.dll程序集中。

All this is particularly important if you have a background with Java, where there is only one concept, the package. 如果你有一个Java背景,那里只有一个概念,即包,这一切尤为重要。 In .NET there are two, the assembly and the namespace. 在.NET中有两个,即程序集和命名空间。 The two are orthogonal. 两者是正交的。 An assembly can obviously contain more than one namespace. 程序集显然可以包含多个名称空间。 An assembly can reopen a namespace and add more to it - in other words, the types in a namespace may span more than one assembly. 程序集可以重新打开命名空间并向其添加更多内容 - 换句话说,命名空间中的类型可能跨越多个程序集。

Assembly is physical grouping of logical units, Namespace, logically groups classes. 程序集是逻辑单元的物理分组,命名空间,逻辑组类。

Namespace can span multiple assembly 命名空间可以跨多个程序集

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

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