可能是重复问题:
C#:为什么我的公共类不能扩展内部类?
如果此问题以前已经被提出,我很抱歉。我在Stack Overflow上搜索过,但没有找到。
我只是好奇这种设计背后的原理是什么。显然,我理解基类型的私有/内部成员不能暴露通过派生公共类型。但是,在我的天真思考中,似乎“隐藏”的部分可以保持隐藏,同时仍共享一些基本功能并公开新接口。
我考虑以下内容:
Assembly X
internal class InternalClass
{
protected virtual void DoSomethingProtected()
{
// Let's say this method provides some useful functionality.
// Its visibility is quite limited (only to derived types in
// the same assembly), but at least it's there.
}
}
public class PublicClass : InternalClass
{
public void DoSomethingPublic()
{
// Now let's say this method is useful enough that this type
// should be public. What's keeping us from leveraging the
// base functionality laid out in InternalClass's implementation,
// without exposing anything that shouldn't be exposed?
}
}
装配 Y
public class OtherPublicClass : PublicClass
{
// It seems (again, to my naive mind) that this could work. This class
// simply wouldn't be able to "see" any of the methods of InternalClass
// from AssemblyX directly. But it could still access the public and
// protected members of PublicClass that weren't inherited from
// InternalClass. Does this make sense? What am I missing?
}