为什么 C# 中的类不能扩展它自己的嵌套类?
例如:
public class A : A.B
{
public class B { }
}
编译器会生成此错误:
循环基类依赖 涉及“A”和“AB”
我总是认为嵌套类的行为就像常规类一样,除了有关访问外部类的私有成员的特殊规则,但我猜这两个类之间存在一些隐式继承?
For example:
public class A : A.B
{
public class B { }
}
Which generates this error from the compiler:
Circular base class dependency
involving 'A' and 'A.B'
I always figured a nested class behaved just like a regular class except with special rules concerning accessing the outer class's private members, but I guess there's some implicit inheritance occurring between the two classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
据我所知,不涉及隐式继承。 我本以为这没什么问题——尽管我可以想象如果 A 和 B 是通用的会很奇怪。
它在规范的第 10.1.4 节中指定:
我已经突出显示了相关部分。
这解释了为什么编译器拒绝它,但不能解释为什么语言禁止它。 我想知道是否有 CLI 限制...
编辑:好的,我已经收到了 Eric Lippert 的回复。 基本上,它在技术上是可行的(CLI 中没有任何内容可以禁止它),但是:
电子邮件线程中还指出,这将使此类事情有效:
...但是如果 B 派生自 A,那么这已经(如 Tinister 所指出的)有效。
嵌套 + 继承 = 奇怪...
There's no implicit inheritance involved as far as I can tell. I would have expected this to be okay - although I can imagine weirdness if A and B were generic.
It's specified in section 10.1.4 of the spec:
I've highlighted the relevant section.
That explains why the compiler is rejecting it, but not why the language prohibits it. I wonder if there's a CLI restriction...
EDIT: Okay, I've had a response from Eric Lippert. Basically, it would be technically possible (there's nothing in the CLI to prohibit it), but:
It was also noted on the email thread that it would make this kind of thing valid:
... but that would already (as noted by Tinister) be valid if B derived from A.
Nesting + inheritance = oddness...
这不是 C# 的事情,而是编译器的事情。 编译器的工作之一是在内存中布局一个类,即一堆基本数据类型、指针、函数指针和其他类。
在知道 B 类的布局是什么之前,它无法构造 A 类的布局。 在完成 A 类的布局之前,它无法知道 B 类的布局是什么。 循环依赖。
This is not a C# thing as much as it is a compiler thing. One of the jobs of a compiler is to lay out a class in memory, that is a bunch of basic data types, pointers, function pointers and other classes.
It can't construct the layout for class A until it knows what the layout of class B is. It can't know what the layout of class B is until it finished with the layout of class A. Circular dependency.
关于我试图做什么的问题:
基本上,我想创建一个与其自身具有组合关系的类,但我不想让包含的对象包含其他对象,因此创建一个包含许多“A”的链有一个 A 有一个 A 有一个 A 有一个...”关系。 所以我当时的想法是做这样的事情:
当时看起来相当圆滑,但回想起来我不太确定......
我翻遍了谷歌,没有找到任何好的讨论,所以我想我会把它张贴在这里。
但是,在 Eric Lippert 博客上的帖子 他给出了一个实现嵌套接口的类的示例,以及一个以嵌套类作为类型参数实现通用接口的类的示例(该类无法编译,他调用了“当前编译器中的错误”)。 这两个例子都涉及接口,所以我想知道嵌套类是否有一些特殊的规则。 似乎是有的。
Regarding questions about what I was attempting to do:
Basically, I wanted to create a class that had a composition relationship with itself, but I didn't want to have the contained object to contain other objects and therefore create a chain with many "A has-a A has-a A has-a A has-a..." relationships. So my thought at the time was do something like this:
Which at the time seemed pretty slick but in retrospect I'm not so sure...
I had rummaged through Google and didn't find any good discussion on it so I thought I'd post it here.
However, within the comments of a post at Eric Lippert's Blog he gives examples of a class implementing a nested interface as well as a class implementing a generic interface with a nested class as the type argument (which doesn't compile and he calls a "bug" in the current compiler). Both of those examples concern interfaces so I was wondering if there was some special rules with nested classes. And it seems there are.
我认为嵌套意味着嵌套类型是嵌套类型定义的一部分。 通过这种解释,这个限制是有意义的,因为当编译器到达 A 的定义时,AB 尚未定义,甚至在 A 的末尾,它已经根据 AB 定义了
I think the nesting is meant to represent that the nested type is part of the definition of the nesting type. With that interpretation, the limitation makes sense because at the time the compiler hits the definition of A, A.B is not yet defined, and even at the end of A, it is already defined in terms of A.B.
通过从包含嵌套接口的单独类继承,我能够避免这种情况(至少对于接口)。 (在我的场景中,我还返回对这些接口的引用。)
而不是:
执行以下操作:
为了避免显式接口实现的丑陋,您还可以从其他类继承,尽管如果您尝试这样做,这将不起作用从嵌套类继承,因为您不能从这两个类继承。
I was able to avoid this (at least with interfaces) by inheriting from a separate class containing the nested interfaces. (In my scenario I am also returning references to these interfaces.)
Instead of:
Do something like this:
To avoid the ugliness with explicit interface implementations, you can also inherit from the other class, though that wouldn't work if you were trying to inherit from a nested class, since you can't inherit from both classes.
这对我来说毫无意义......你试图扩展一些不存在的东西! B类只存在于A类的范围内,因此我认为存在某种继承。
This makes no sense to me... You are trying to extend something that doesn't exist !!! Class B only exists in the scope of class A and because of this I think there is some kind of inheritance.