C#8基本接口的默认方法调用解决方法 [英] C# 8 base interface's default method invocation workaround

查看:251
本文介绍了C#8基本接口的默认方法调用解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods
可以显式调用接口库

According to https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods It is possible to explicitly invoke an interface base implementation with the following syntax.

base(IInterfaceType).Method();

但这似乎尚未实现。

是否有解决方法(例如反射)来实现这一目标?

Is there a workaround (e.g reflection) to achieve this?


说明问题的示例代码

Example code to illustrate the problem



interface IA
{
    void M()
    {
        Console.WriteLine("IA.M");
    }
}

interface IB : IA
{
    void IA.M()
    {
        Console.WriteLine("IB.M");
    }
}

interface IC : IA
{
    void IA.M()
    {
        Console.WriteLine("IC.M");
    }
}

class D : IA, IB, IC
{
    public void M()
    {
        // base(IB).M(); Is not yet supported apparently
        ((IB)this).M(); // Throws stack overflow
    }
}

class Program
{
    static void Main(string[] args)
    {
        D d = new D();
        d.M();
    }
}


推荐答案

问题中的链接指向复制的提案版本来自Github中的提案文档

The link in the question points to a version of the proposal copied from the proposal document in Github

此功能为在2019年4月切割


结论

剪切C#8的base()语法。我们打算在下一个主要版本中重新使用它。 p>

Cut base() syntax for C# 8. We intend to bring this back in the next major release.

设计会议文档解释说,如果没有运行时支持(将无法及时提供),则该实现对于C#最多是可行的但不是VB.NET。

The design meeting doc explains that without runtime support (which wouldn't be available in time), the implementation would be workable at best for C# but not VB.NET.


如果运行时不存在B.M,则会调用A.M()。对于base()和interfaces,运行时不支持此功能,因此调用将引发异常。我们想在运行时中添加对此的支持,但是发布此版本太昂贵了。

If B.M is not present at run time, A.M() will be called. For base() and interfaces, this is not supported by the runtime, so the call will throw an exception instead. We'd like to add support for this in the runtime, but it is too expensive to make this release.

我们有一些解决方法,但是它们没有行为。我们想要的,而不是首选的代码生成器。尽管不是我们想要的那样,我们对C#的实现还是可行的,但是VB的实现会困难得多。此外,VB的实现要求接口实现方法必须是公共API表面。

We have some workarounds, but they do not have the behavior we want, and are not the preferred codegen. Our implementation for C# is somewhat workable, although not exactly what we would like, but the VB implementation would be much more difficult. Moreover, the implementation for VB would require the interface implementation methods to be public API surface.

对于无限递归来说,

public void M()
{
    ((IB)this).M(); // Throws stack overflow
}

本质上就是

public void M()
{
    M(); // Throws stack overflow
}

默认接口成员的调用与显式实现接口的方式相同方法是通过接口。此外,您要求在 this 而不是 base 上调用该方法。

Default interface members are called the same way explicitly implemented interface methods are, through the interface. Besides, you're asking to call the method on this, not base.

这篇关于C#8基本接口的默认方法调用解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆