c# - C# 中 "internal"关键字的实际用途

标签 c# .net-assembly access-modifiers internals

能否请您解释一下 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/

相关文章:

c# - 如何使用 C# 为 sql server express 添加异常到 Windows 防火墙

c# - 如何解决 Visual Studio 中的 CA 1009 警告

versioning - 使 TeamCity 版本与 .NET 程序集版本匹配

c# - 带有程序集引用的 asp.net 3.5 引用项目在单元测试中抛出签名/强名称错误

java - 我需要一个可以从我的 API 中的任何地方访问但不能从使用 API 的应用程序访问的 java 方法

c# - 我的程序如何知道 Windows 已重新启动以进行更新

c# - 对象列表的回发为空

c# - 在 ASP.NET 代码隐藏中声明一个全局变量

c# - 使用私有(private) setter 测试属性

c# - 带有抽象的装饰器,不能正确获取装饰值