简体   繁体   English

可以在其他命名空间中访问内部类吗?

[英]Can internal classes be accessed within other namespaces?

I'm currently reading this book online: http://www.angelfire.com/theforce/chewy0/csharp/Thinking_in_C-Sharp_.pdf我目前正在在线阅读这本书: http : //www.angelfire.com/theforce/chewy0/csharp/Thinking_in_C-Sharp_.pdf

On page 23 (38 of the PDF document) it states that internal classes cannot be accessed from classes within external namespaces .在第 23 页(PDF 文档的第 38 页),它指出无法从外部命名空间内的类访问内部类。

However, in the online Microsoft documentation it states that internal classes are only accessible from the same assembly .但是,在 Microsoft 在线文档中,它指出内部类只能从同一个程序集访问。

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/internal https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/internal

From my understanding, an assembly can contain classes from multiple namespaces .根据我的理解,程序集可以包含来自多个命名空间的类。 Would this not mean from the Microsoft documentation that internal classes could be accessed across different namespaces ?这是否意味着从 Microsoft 文档中可以跨不同的命名空间访问内部类?

Or is it true to say that internal classes are private within both of their respected assemblies and namespaces ?还是说内部类在其受尊重的程序集命名空间中都是私有的?

So this is the excerpt, right?所以这是摘录,对吧?

Java uses five explicit keywords to set the boundaries in a class: public, private, protected, internal, and protected internal. Java 使用五个显式关键字来设置类中的边界:public、private、protected、internal 和 protected internal。 Their use and meaning are quite straightforward.它们的用法和含义非常简单。 These access specifiers determine who can use the definitions that follow.这些访问说明符确定谁可以使用后面的定义。 public means the following definitions are available to everyone. public 意味着每个人都可以使用以下定义。 The private keyword, on the other hand, means that no one can access those definitions except you, the creator of the type, inside member functions of that type.另一方面,private 关键字意味着除了您(类型的创建者)在该类型的成员函数内部之外,没有人可以访问这些定义。 private is a brick wall between you and the client programmer. private 是你和客户端程序员之间的一堵砖墙。 If someone tries to access a private member, they'll get a compile-time error.如果有人试图访问私有成员,他们将收到编译时错误。 protected acts like private, with the exception that an inheriting class has access to protected members, but not private members.受保护的行为类似于私有,除了继承类可以访问受保护成员,但不能访问私有成员。 Inheritance will be introduced shortly.继承将很快介绍。 internal is often called “friendly”–the definition can be accessed by other classes in the same namespace as if it were public, but is not accessible to classes in different namespaces. internal 通常被称为“友好的”——定义可以被同一命名空间中的其他类访问,就好像它是公共的一样,但不能被不同命名空间中的类访问。 Namespaces will be discussed in depth in chapter #ref# [sic].命名空间将在#ref# [sic] 一章中深入讨论。 protected internal allows access by classes within the same namespace (as with internal) or by inheriting classes (as with protected) even if the inheriting classes are not within the same namespace. protected internal 允许相同命名空间内的类(如 internal)或继承类(如 protected)访问,即使继承类不在同一命名空间内。

C#'s default access, which comes into play if you don't use one of the aforementioned specifiers, is internal C# 的默认访问,如果您不使用上述说明符之一,就会发挥作用,是内部的

The author is probably conflating Java's internal with c#'s internal .笔者大概混为一谈Java的internal用C#的internal

They are slightly different, because Java does not have assemblies;它们略有不同,因为 Java 没有程序集; it has packages, which organize classes into namespaces.它有包,将类组织到命名空间中。

In c#, namespace has absolutely no relationship with accessibility modifiers.在 c# 中,命名空间与可访问性修饰符完全没有关系。 Only classes within the same assembly can access an internal type or member, unless you use the InternalsVisibleTo attribute.只有同一程序集中的类才能访问internal类型或成员,除非您使用InternalsVisibleTo属性。 Namespace doesn't matter.命名空间无关紧要。

Well, the easiest way to answer this was to test it- So I've made 2 namespaces within 1 assembly, and accessed an internal variable.嗯,回答这个问题的最简单方法是测试它 - 所以我在 1 个程序集中创建了 2 个命名空间,并访问了一个内部变量。

Short answer- the Microsoft documentation is correct- it's possible to access an internal variable within the same assembly, even when you have 2 different namesapces.简短的回答 - Microsoft 文档是正确的 - 即使您有 2 个不同的命名空间,也可以访问同一程序集中的内部变量。

Here's the code:这是代码:

namespace ConsoleApplication1
{
    class Class1
    {
        internal string thing;
        public Class1()
        {
            thing = "original data";
        }
    }
}

namespace ConsoleApplication2
{
    class Class2
    {
        public ConsoleApplication1.Class1 class1= new ConsoleApplication1.Class1();
        public Class2()
        {
            class1.thing = "other namespace modification";
        }
    }
}

When calling Class2 constructor, the modified data was displayed.调用 Class2 构造函数时,显示修改后的数据。

var class2 = new ConsoleApplication2.Class2();
Console.WriteLine(class2.class1.thing);

Result:结果:

"other namespace modification"

Hope this helps :)希望这有帮助:)

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

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