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

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

问题描述

根据https://docs.microsoft.com/en-us/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?

说明问题的示例代码

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

该功能是 4 月2019

结论

削减 C# 8 的 base() 语法.我们打算在下一个主要版本中恢复它.

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() 和接口,运行时不支持,因此调用将抛出异常.我们想在运行时添加对此的支持,但发布此版本的成本太高了.

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天全站免登陆