【问题标题】:C# How to reference default interface implementation in implementer class [duplicate]C#如何在实现类中引用默认接口实现[重复]
【发布时间】:2021-11-22 18:03:54
【问题描述】:

考虑以下接口,默认实现为TestMethod

public interface TestInterface
{
    public int TestMethod()
    {
        return 15;
    }
}

在下面的类中调用TestMethod会导致StackOverflowException:

public class TestClass : TestInterface
{
    public int TestMethod()
    {
        return 1 + (this as TestInterface).TestMethod();
    }
}

现在我明白为什么会这样了,但是有什么办法可以绕过它吗? base.TestMethod() 之类的东西用于引用该类的已实现接口之一?

我知道我可以重命名 TestInterface 中的方法并以这种方式在 TestClass 中引用它,但这会导致其他不需要引用默认实现的类出现问题。

【问题讨论】:

标签: c# interface default-implementation


【解决方案1】:

您需要使用“公共覆盖”来执行您的要求。

【讨论】:

  • 能否请您用具体的代码 sn-p 和更多描述详细说明您的答案。
猜你喜欢
  • 2022-12-17
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 2012-03-01
  • 2014-10-01
  • 2015-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多