如何调用默认方法而不是具体实现 [英] How can I call the default method instead of the concrete implementation

查看:119
本文介绍了如何调用默认方法而不是具体实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在C#8中更改了默认接口方法的行为?
过去,以下代码(未发布默认接口方法时):

Why is the behavior of Default Interface Methods changed in C# 8? In the past the following code (When the Default interface methods was demo not released):

interface IDefaultInterfaceMethod
{
    // By default, this method will be virtual, and the virtual keyword can be here used!
    virtual void DefaultMethod()
    {
        Console.WriteLine("I am a default method in the interface!");
    }

}

interface IOverrideDefaultInterfaceMethod : IDefaultInterfaceMethod
{
    void IDefaultInterfaceMethod.DefaultMethod()
    {
        Console.WriteLine("I am an overridden default method!");
    }
}

class AnyClass : IDefaultInterfaceMethod, IOverrideDefaultInterfaceMethod
{
}

class Program
{
    static void Main()
    {
        IDefaultInterfaceMethod anyClass = new AnyClass();
        anyClass.DefaultMethod();

        IOverrideDefaultInterfaceMethod anyClassOverridden = new AnyClass();
        anyClassOverridden.DefaultMethod();
    }
}

具有以下输出:

控制台输出:


我是界面中的默认方法!

我是一个覆盖的默认方法!

I am a default method in the interface!
I am an overridden default method!

但是使用C#8最新版本时,上面的代码将产生以下输出:

But with the C# 8 last version the code above is producing the following output:

控制台输出:


我是被覆盖的默认方法!

我是被覆盖的默认方法!

I am an overridden default method!
I am an overridden default method!

任何人都可以向我解释为什么这种行为会改变吗?

Anyone can explain to me why this behavior is changed?

注意:

IDefaultInterfaceMethod anyClass = new AnyClass(); anyClass.DefaultMethod();

((IDefaultInterfaceMethod) anyClass).DefaultMethod(); // STILL the same problem!??


推荐答案

我怀疑有一个更好的问题是:

I suspect a better question would be:


如何调用默认方法而不是具体实现?

How can I call the default method instead of the concrete implementation?

该功能是已计划但已从2019年4月的C#8中删除,因为有效的实现需要运行时的支持。无法在发布前及时添加。该功能必须在C#和VB.NET上都能正常运行-F#仍然不喜欢接口。

The feature was planned but was cut from C# 8 in April 2019, because an efficient implementation would require support from the runtime. This couldn't be added in time before release. The feature would have to work well both for C# and VB.NET - F# doesn't like interfaces anyway.


如果BM在运行时不存在, AM()将被调用。对于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.

我们有一些解决方法,但是它们没有我们想要的行为,并且不是首选的代码生成器。

We have some workarounds, but they do not have the behavior we want, and are not the preferred codegen.

我们的C#实现在某种程度上是可行的,尽管并不是我们想要的那样,但是VB实现会困难得多。此外,VB的实现将要求接口实现方法是公共API表面。

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.

它将可以通过 base()调用类似于类的工作方式。复制提案示例:

It will work through a base() call similar to how classes work. Coopying the proposal example :

interface I1
{ 
    void M(int) { }
}

interface I2
{
    void M(short) { }
}

interface I3
{
    override void I1.M(int) { }
}

interface I4 : I3
{
    void M2()
    {
        base(I3).M(0) // What does this do?
    }
}

这篇关于如何调用默认方法而不是具体实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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