相关疑难解决方法(0)

使成员成为虚拟成员可防止调用默认接口实现并导致 C# 8 中的 StackOverflowException

考虑代码:

class ChildClass : BaseClass {

    public void Method1() {} //some other method

}

abstract class BaseClass : IChildInterface {

    public
    virtual //<- If we add virtual so that this method can be overridden by ChildClass, we get StackOverflowException and DoWork() implementation in IChildInterface is never called.
    void DoWork() {
     //base class specific implmentation
     ((IChildInterface)this).DoWork(); //call into default implementation provided by IChildInterface
    }

}

interface IChildInterface : IBaseInterface {

    void IBaseInterface.DoWork() {
     //implmentation
    }

}

interface IBaseInterface {

    void DoWork(); …
Run Code Online (Sandbox Code Playgroud)

.net c# c#-8.0 .net-core-3.0 default-interface-member

5
推荐指数
1
解决办法
212
查看次数