能否请您解释一下 C# 中 internal
关键字的实际用法?
我知道 internal
修饰符限制对当前程序集的访问,但我应该在什么时候以及在什么情况下使用它?
最佳答案
您希望从同一程序集中的许多其他类访问的实用程序或帮助程序类/方法,但您希望确保其他程序集中的代码无法访问。
来自 MSDN (通过 archive.org):
A common use of internal access is in component-based development because it enables a group of components to cooperate in a private manner without being exposed to the rest of the application code. For example, a framework for building graphical user interfaces could provide Control and Form classes that cooperate using members with internal access. Since these members are internal, they are not exposed to code that is using the framework.
您还可以将内部修饰符与 InternalsVisibleTo
一起使用程序集级别属性,用于创建被授予对目标程序集内部类的特殊访问权限的“友元”程序集。
这对于创建单元测试程序集很有用,然后允许调用要测试的程序集的内部成员。当然,没有其他程序集被授予此级别的访问权限,因此当您发布系统时,封装将保持不变。
关于c# - C# 中 "internal"关键字的实际用途,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35776190/