C#8基本接口的默认方法调用解决方法

编程入门 行业动态 更新时间:2024-10-25 18:32:49
本文介绍了C#8基本接口的默认方法调用解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

According to docs.microsoft/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基本接口的默认方法调用解决方法

本文发布于:2023-11-04 17:39:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1558583.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:解决方法   接口   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!