C# 程序集,程序集中有什么?
我试图了解 C# 中的内部访问修饰符。我似乎无法理解程序集到底是什么,以及我的程序的哪一部分包含在该程序集中。我试图做到这一点,以便变量只能由以下命名空间内的对象访问:
namespace Engine.Entity
所讨论的变量是在该命名空间内的类中定义的,所以我假设如果我将其设为内部,则只有该命名空间内的对象才具有访问它。我将程序集和命名空间视为一体,但我认为这是不对的。
I'm trying to understand the internal access modifier in C#. I can't seem to understand what an assembly is exactly, and what part of my program is held inside that assembly. I was trying to make it so that a variable is accessibly only by objects within the following namespace:
namespace Engine.Entity
The variable in question is defined in a class inside of that namespace, so I assumed if I made it internal, only objects inside of that namespace have access to it. I am seeing assemblies and namespaces as one, and I don't think that's right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
基本上,您不能使变量仅在给定名称空间内可见。由于任何人都可以定义任何命名空间,这将使
internal
的想法无效:您只需编写即可访问定义为
internal
的任何变量、类或方法例如,System
命名空间。Basically, you cannot make a variable visible only from within a given namespace. As anybody can define any namespace, this would make the idea of
internal
void: you would just have to writeto gain access to any variable, class or method defined as
internal
in theSystem
namespace, for instance.命名空间仅影响名称解析。命名空间并不意味着任何类型的存储,命名空间也不会确定哪些 DLL 包含您的代码。命名空间允许您将相关的事物以逻辑名称分组在一起,即使它们实际上可能驻留在不同的 DLL 中。
程序集基本上只是一个 DLL 或 EXE 文件。它包含 IL 代码和描述该 DLL 或 EXE 中代码的类型信息。它也可以包含许多其他内容,但对于初学者来说,只需将其视为 DLL。
通过将代码编译到生成 DLL 或 EXE 的项目 (csproj) 中,可以将代码放入特定的程序集中。
命名空间可以跨越多个程序集。也就是说,作为该逻辑名称空间成员的类可以驻留在多个 DLL 中。仅当您的项目引用包含该类的正确程序集 (DLL) 时,您才能访问源代码中的特定类。
Internal 修饰符意味着只能从同一程序集中访问该符号。只有编译到与您的代码相同的 DLL 中的代码才能访问带有内部标记的属性或方法。
Namespaces affect name resolution only. Namespaces do not imply any sort of storage, nor do namespaces determine which DLLs contain your code. Namespaces allow you to group related things together under a logical name even though they may physically reside in different DLLs.
An assembly is basically just a DLL or EXE file. It contains IL code and type information that describes the code in that DLL or EXE. It can contain a lot of other stuff too, but for starters just think of it as a DLL.
You put your code into a particular assembly by compiling your code into a project (csproj) that produces the DLL or EXE.
A namespace can span multiple assemblies. That is, classes that are members of that logical namespace may reside in multiple DLLs. You can access a particular class in your source code only if your project references the correct assembly (DLL) that contains that class.
The Internal modifier means that the symbol can only be accessed from within the same assembly. Only code that is compiled into the same DLL as your code can access your properties or methods that are tagged with internal.
人们很容易对命名空间/程序集的事情感到困惑,因为它解耦了代码的物理位置(程序集)以及如何引用它的概念(逻辑引用是通过使用命名空间,物理引用是通过引用程序集)。
我通常使用“贡献”一词来解释这一点:
一个程序集可以为多个命名空间做出贡献。
例如,
System.Data.dll
程序集贡献了System.Data
等命名空间(例如,System.Data.DataTable
类)和 < code>Microsoft.SqlServer.Server(例如类Microsoft.SqlServer.Server.SqlContext
)。多个程序集可以贡献于一个命名空间。
例如,
System.Data.dll
程序集和System.Xml.dll
程序集都贡献于System.Xml
命名空间。这意味着,如果您使用项目中的 System.Xml.XmlDataDocument 类,则需要引用 System.Data.dll 程序集。
如果您使用
System.Xml.XmlDocument
类,则需要从项目中引用System.Xml.dll
。(上面的示例是 .NET 4.0,但也可能适用于以前的 .NET 版本)。
丹尼·索普 很好地解释了
命名空间
和内部
的概念,所以我不会详细介绍这些。——杰罗恩
People are easily confused by the namespace/assembly thing, as it decouples the concept of where your code is physically located (the assembly) and how you reference it (logically reference is by using the namespace and physical reference is by referencing the assembly).
I usually explain this using the word
contribute
:An assembly can contribute to multiple namespaces.
For instance, the
System.Data.dll
assembly contributes to namespaces likeSystem.Data
(e.g. the classSystem.Data.DataTable
) andMicrosoft.SqlServer.Server
(e.g. the classMicrosoft.SqlServer.Server.SqlContext
).Multiple assemblies can contribute to a single namespace.
For instance both the
System.Data.dll
assembly and theSystem.Xml.dll
assembly contribute to theSystem.Xml
namespace.Which means that if you use the
System.Xml.XmlDataDocument
class from your project, you need to reference theSystem.Data.dll
assembly.And if you use the
System.Xml.XmlDocument
class, you need to reference theSystem.Xml.dll
from your project.(the above examples are .NET 4.0, but likely hold for previous .NET versions as well).
Danny Thorpe explained the concept of
namespace
andinternal
really well, so I won't go into detail about those.--jeroen
来自内部(C# 参考)
因此这意味着来自相同的程序集/dll,而不是 命名空间。
From internal (C# Reference)
So this means from within the same assembly/dll, not namespace.
命名空间和程序集不是同义词。通常,一个命名空间跨越多个程序集。从 Visual Studio 构建的任何托管代码都具有项目、程序集和 DLL/EXE 二进制文件的一一对应关系。
但是,如果将托管代码与命令行链接,则可以创建一个程序集,其中多个项目文件都属于一个程序集(这意味着磁盘上的多个文件一起代表一个程序集)。但不要介意这种情况,这是一个深奥的事情,在实践中永远不会发生。
“内部”访问修饰符仅意味着只能从该程序集内部访问目标。它与命名空间无关。
Namespaces and assemblies are not synonymous. Often a namespace spans several assemblies. Any managed code built from Visual studio has a one for one corresponce of projects to assemblies to DLL/EXE binaries.
However, if you link your managed code with the command line, you can make an assembly where multiple project files all belong to one assembly (which means multiple files on your disk are together representing one assembly). But never mind this case, it is an esoteric thing that never occurs in practice.
The 'internal' access modifer simply means that the target can only be accessed from within that assembly. It has no bearing on namespaces.