C# 使 class 对除其自身以外的所有名称空间不可见

C# make class invisible to all namespaces other than its own

假设我有命名空间 Events,它只包含 classes EventManagerEventType.

我希望 EventType 在任何其他命名空间中不可见,唯一应该能够看到 EventType 的 class 是 EventManager

我该怎么做?我读了一些关于 Friend 的内容,但仍然不知道这是正确的选择。

你在这个XY Problem(同时)声明中表达了两个"wants":

I want EventType to be invisible in any other namespace, the only class who should be able to see EventType is EventManager.

对于第一部分(X),如评论中所述,无法直接将对 class 的访问限制为仅单个命名空间C#。如果您将它 internal 并将其隔离在包含 该命名空间的程序集中,您可以得到您想要的。

然而,这似乎是一种可疑的做法,当您着手添加代码时,您可能最终会放弃这种方法。

对于第二部分 (Y),您可以让 EventType 仅对 EventManager 可见(这是您的真实意图) 这样

public class EventManager
{
     private class EventType
     { ... }
}

朋友idea顺便来自C++