C# 程序集,程序集中有什么?

发布于 2024-10-08 03:57:22 字数 230 浏览 10 评论 0原文

我试图了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

权谋诡计 2024-10-15 03:57:23

基本上,您不能使变量仅在给定名称空间内可见。由于任何人都可以定义任何命名空间,这将使 internal 的想法无效:您只需编写

namespace System
{
    public static MySystemInternalSpy
    {
        public static void SpyInternals()
        {
            ...
        }
    }
}

即可访问定义为 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 write

namespace System
{
    public static MySystemInternalSpy
    {
        public static void SpyInternals()
        {
            ...
        }
    }
}

to gain access to any variable, class or method defined as internal in the System namespace, for instance.

绿萝 2024-10-15 03:57:22

命名空间仅影响名称解析。命名空间并不意味着任何类型的存储,命名空间也不会确定哪些 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.

忱杏 2024-10-15 03:57:22

人们很容易对命名空间/程序集的事情感到困惑,因为它解耦了代码的物理位置(程序集)以及如何引用它的概念(逻辑引用是通过使用命名空间,物理引用是通过引用程序集)。

我通常使用“贡献”一词来解释这一点:

  1. 一个程序集可以为多个命名空间做出贡献
    例如,System.Data.dll 程序集贡献了 System.Data 等命名空间(例如,System.Data.DataTable 类)和 < code>Microsoft.SqlServer.Server(例如类 Microsoft.SqlServer.Server.SqlContext)。

  2. 多个程序集可以贡献于一个命名空间
    例如,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:

  1. An assembly can contribute to multiple namespaces.
    For instance, the System.Data.dll assembly contributes to namespaces like System.Data (e.g. the class System.Data.DataTable) and Microsoft.SqlServer.Server (e.g. the class Microsoft.SqlServer.Server.SqlContext).

  2. Multiple assemblies can contribute to a single namespace.
    For instance both the System.Data.dll assembly and the System.Xml.dll assembly contribute to the System.Xml namespace.
    Which means that if you use the System.Xml.XmlDataDocument class from your project, you need to reference the System.Data.dll assembly.
    And if you use the System.Xml.XmlDocument class, you need to reference the System.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 and internal really well, so I won't go into detail about those.

--jeroen

万劫不复 2024-10-15 03:57:22

来自内部(C# 参考)

内部关键字是一个访问
类型和类型成员的修饰符。
内部类型或成员是
只能在以下文件中访问
相同的程序集

因此这意味着来自相同的程序集/dll,而不是 命名空间

From internal (C# Reference)

The internal keyword is an access
modifier for types and type members.
Internal types or members are
accessible only within files in the
same assembly

So this means from within the same assembly/dll, not namespace.

狼性发作 2024-10-15 03:57:22

命名空间和程序集不是同义词。通常,一个命名空间跨越多个程序集。从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文