在 C# 8.0 中,有没有办法公开 class 上的默认成员实现?

In C# 8.0 is there a way to expose default member implementation on the class?

在 C# 8.0 中,接口可以为某些成员提供默认实现。这些实现是明确的,意味着它们无法通过 class 实例访问,我们必须转换为接口类型才能调用它们。有没有办法在同名的 class 实例上公开此实现?可以通过将 this 指针转换为接口类型并调用如下方法来以另一个名称公开它:

void A()
{
   ((ISomething)this).B();
}

但是我似乎无法找到一种方法来公开具有 B 的原始名称的实现,因为如果我声明一个方法 B,它会被视为实现导致无限递归的接口的方法。有没有什么方法可以在不复制的情况下公开实现,或者我遗漏了什么?

澄清一下,我正在寻找一种方法来实现类似特征的功能,即能够将实现从接口直接导入 class 的 public API不更改方法名称(大概接口中的名称是最好的)。问题不在于如何以 class 的用户身份调用该方法,而是如何使其成为 class.[=12= 的 public API 的一部分]

扩展方法是一种解决方案,但默认接口实现可以访问受保护的成员并且可以重载。

没有简单的方法;可能有各种丑陋的解决方法,所有这些都归结为委托给其他方法(静态方法或依赖本身未实现该方法的帮助程序 class)来访问它,即使那样你也需要传入以某种方式陈述。有一个 proposal for a base(T) syntax, draft specification here, which should allow base(ISomething).B() to refer to the implementation without causing a cyclic reference. This was originally slated to be part of C# 8, but this proved to be too ambitious and it was cut. As of writing, it's a candidate for inclusion in C# 9.

一种可能是为它们提供属性。当我需要从 class.

中引用显式实现时,这就是我所做的
class Something : IInterfaceA, IInterfaceB {
   public IInterfaceA A => this;
   public IInterfaceB B => this;
}

...

something.A.AMethod();
something.B.BMethod();

您也可以考虑使用扩展方法而不是默认实现。反正都有些相似。

interface IInterface {
}

static class IInterfaceExtensions {
   public static void DoSomething(
      this IInterface me
   ) {
      // do something
   }
}