internal 类 可以在其他命名空间中访问吗?
Can internal classes be accessed within other namespaces?
我目前正在在线阅读这本书:
http://www.angelfire.com/theforce/chewy0/csharp/Thinking_in_C-Sharp_.pdf
在第 23 页(PDF 文档的第 38 页)它指出 internal 类 无法从外部 类 访问 命名空间.
但是,在在线 Microsoft 文档中它指出 internal 类 只能从相同的 assembly 访问。
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/internal
根据我的理解,程序集 可以包含来自多个 命名空间 的 类。
这是否意味着 Microsoft 文档中 internal 类 可以跨不同的 namespaces 访问?
或者说 internal 类 在他们受人尊敬的 assemblies 和 [=25] 中都是私有的=]命名空间?
嗯,回答这个问题最简单的方法是测试它-
所以我在 1 个程序集中创建了 2 个命名空间,并访问了一个内部变量。
简短回答 - Microsoft 文档是正确的 - 可以访问同一程序集中的内部变量,即使您有 2 个不同的命名空间。
代码如下:
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";
}
}
}
调用Class2构造函数时,显示修改后的数据
var class2 = new ConsoleApplication2.Class2();
Console.WriteLine(class2.class1.thing);
结果:
"other namespace modification"
希望这对您有所帮助:)
这是摘录,对吧?
Java uses five explicit keywords to set the boundaries in a class: public,
private, protected, internal, and 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. 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 is a
brick wall between you and the client programmer. 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. Namespaces will
be discussed in depth in chapter #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.
C#’s default access, which comes into play if you don’t use one of the
aforementioned specifiers, is internal
作者可能将 Java 的 internal
与 c# 的 internal
混为一谈。
它们略有不同,因为Java没有程序集;它有包,将 类 组织到命名空间中。
在c#中,命名空间与可访问性修饰符完全没有关系。只有同一程序集中的 类 才能访问 internal
类型或成员,除非您使用 InternalsVisibleTo 属性。命名空间无关紧要。
我目前正在在线阅读这本书: http://www.angelfire.com/theforce/chewy0/csharp/Thinking_in_C-Sharp_.pdf
在第 23 页(PDF 文档的第 38 页)它指出 internal 类 无法从外部 类 访问 命名空间.
但是,在在线 Microsoft 文档中它指出 internal 类 只能从相同的 assembly 访问。
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/internal
根据我的理解,程序集 可以包含来自多个 命名空间 的 类。 这是否意味着 Microsoft 文档中 internal 类 可以跨不同的 namespaces 访问?
或者说 internal 类 在他们受人尊敬的 assemblies 和 [=25] 中都是私有的=]命名空间?
嗯,回答这个问题最简单的方法是测试它- 所以我在 1 个程序集中创建了 2 个命名空间,并访问了一个内部变量。
简短回答 - Microsoft 文档是正确的 - 可以访问同一程序集中的内部变量,即使您有 2 个不同的命名空间。
代码如下:
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";
}
}
}
调用Class2构造函数时,显示修改后的数据
var class2 = new ConsoleApplication2.Class2();
Console.WriteLine(class2.class1.thing);
结果:
"other namespace modification"
希望这对您有所帮助:)
这是摘录,对吧?
Java uses five explicit keywords to set the boundaries in a class: public, private, protected, internal, and 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. 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 is a brick wall between you and the client programmer. 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. Namespaces will be discussed in depth in chapter #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.
C#’s default access, which comes into play if you don’t use one of the aforementioned specifiers, is internal
作者可能将 Java 的 internal
与 c# 的 internal
混为一谈。
它们略有不同,因为Java没有程序集;它有包,将 类 组织到命名空间中。
在c#中,命名空间与可访问性修饰符完全没有关系。只有同一程序集中的 类 才能访问 internal
类型或成员,除非您使用 InternalsVisibleTo 属性。命名空间无关紧要。