gpt4 book ai didi

c# - 为什么继承嵌套接口(interface)会导致 C# 中的依赖循环?

转载 作者:行者123 更新时间:2023-12-05 01:23:44 25 4
gpt4 key购买 nike

这个例子编译:

public interface IOuter : IInner {

}

public interface IInner {

}

还有这个:

public class Outer : Outer.IInner {

public interface IInner { }

}

但是当 Outer 是一个接口(interface)时,我们会得到一个编译错误:

public interface IOuter : IOuter.IInner {

public interface IInner { }

}
IOuter.cs(3, 18): [CS0529] Inherited interface 'IOuter.IInner' causes a cycle in the interface hierarchy of 'IOuter'

为什么继承嵌套接口(interface)会出现循环?

IInner 是否以某种方式隐式继承了 IOuter,阻止了 IOuter 实现 IInner?看起来不像,因为这样编译:

public interface IOuter {

string OuterProperty { get; }

public interface IInner {

string InnerProperty { get; }

}

}

public class InnerImplementation : IOuter.IInner {

// We only need to implement IInner's members, not IOuter's,
// so IOuter does not seem to be part of the dependency hierarchy here
public string InnerProperty { get; }

}

C# language spec 的接口(interface)部分我根本看不到任何关于嵌套接口(interface)的信息.嵌套接口(interface)通常只是未定义的行为吗?如果是这样,有什么原因吗?


编辑
查看类型声明的语言规范,here ,它说:

A type_declaration can occur as a top-level declaration in a compilation unit or as a member declaration within a namespace, class, or struct.

这里没有指定接口(interface),所以也许这确实是未定义的行为。尽管如此,如果有人知道嵌套接口(interface)类型是否曾经被考虑过并被拒绝过,我想知道为什么。

  • 请参阅以下答案。我看错了规范的部分。

最佳答案

类存在相同的行为:

// doesn't compile either (with a different error)
public class Outer : Outer.IInner {

public class Inner { }

}

这在规范中指定 here :

It is a compile-time error for a class to depend on itself. For the purpose of this rule, a class directly depends on its direct base class (if any) and directly depends on the nearest enclosing class within which it is nested (if any). Given this definition, the complete set of classes upon which a class depends is the transitive closure of the directly depends on relationship.

另请参阅:Why can't a class extend its own nested class in C#?

不能对接口(interface)执行此操作的限制是此规则的扩展,它也考虑了接口(interface)。您在规范中看不到任何关于此的原因是因为该规范仅适用于 C# 6,并且仅在 C# 8 中添加了编写嵌套接口(interface)的能力。您必须查看 Default Interface Methods。在“C# 8 功能”部分下找到规范的“接口(interface)”部分的新更改。在 binding base clauses 中说明部分:

Interfaces now contain types. These types may be used in the baseclause as base interfaces. When binding a base clause, we may need toknow the set of base interfaces to bind those types (e.g. to lookup inthem and to resolve protected access). The meaning of an interface'sbase clause is thus circularly defined. To break the cycle, we add anew language rules corresponding to a similar rule already in placefor classes.

While determining the meaning of the interface_base of an interface,the base interfaces are temporarily assumed to be empty. Intuitivelythis ensures that the meaning of a base clause cannot recursivelydepend on itself.

We used to have the following rules:

<the rules quoted above>

[...]

We are adjusting them as follows:

When a class B derives from a class A, it is a compile-time error forA to depend on B. A class directly depends on its direct base class(if any) and directly depends on the type within which it isimmediately nested (if any).

When an interface IB extends an interface IA, it is a compile-timeerror for IA to depend on IB. An interface directly depends on itsdirect base interfaces (if any) and directly depends on the typewithin which it is immediately nested (if any).

Given these definitions, the complete set of types upon which a typedepends is the reflexive and transitive closure of the directlydepends on relationship.

关于c# - 为什么继承嵌套接口(interface)会导致 C# 中的依赖循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71930343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com