使用接口和显式实现的函数重载
我有以下接口:
public interface Iface
{
void Sample1();
void Sample1(bool value);
}
实现如下所示。注意:要求 Sample1 的实现是显式的(由于通用约束 voodoo),
public class myCLass : Iface
{
void Iface.Sample1()
{
this.Sample1(true);
}
void Iface.Sample1(bool value)
{
}
}
但是尝试调用重载会导致此错误:
错误 5 'myCLass' 不包含 'Sample1' 的定义,并且没有扩展方法 'Sample1 ' 可以找到接受类型为“myCLass”的第一个参数(您是否缺少 using 指令或程序集引用?) Q:\common\VisualStudio\Charting\Drilldowns.cs 18 15 图表
底线:我想我不确定应该使用什么语法来调用同一接口中的“其他”重载。
I have the following interface:
public interface Iface
{
void Sample1();
void Sample1(bool value);
}
And the implementation is shown below. Note: It is requried that Sample1's implementation be explicit (due to generic constraints voodoo)
public class myCLass : Iface
{
void Iface.Sample1()
{
this.Sample1(true);
}
void Iface.Sample1(bool value)
{
}
}
Trying to call the overload, however, results in this error:
Error 5 'myCLass' does not contain a definition for 'Sample1' and no extension method 'Sample1' accepting a first argument of type 'myCLass' could be found (are you missing a using directive or an assembly reference?) Q:\common\VisualStudio\Charting\Drilldowns.cs 18 15 Charting
Bottom line: I think I'm unsure of the syntax I should be using to call the 'other' overload in the same interface.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您要显式实现接口,因此您需要将自己转换为接口:
或者,如果您不喜欢内联转换:
如果您执行了正常的隐式实现,那么您' d 能够直接调用该方法。显式接口实现仅可用于专门定义为该接口的变量。
Since you're implementing the interface explicitly, you need to cast yourself to the interface:
Or, if you don't like the in-line cast:
If you did a normal, implicit implementation, you'd be able to call the method directly. Explicit interface implementations are only usable on a variable specifically defined as that interface.
显式接口实现不是公共方法。
您只能通过转换到接口来调用它们:
Explicit interface implementations aren't public methods.
You can only call them by casting to the interface:
我认为您的代码因重命名而混乱,或者其中有一些代码未包含在问题中(DFResults,IDrilldown)。我假设 DFResults 应该是 MyClass,IDrilldown 应该是 Iface。
所以你有一个接口 IFace,它声明了 2 个方法。您有一个实现该接口的类,并且明确地实现该接口。您似乎接受它必须是显式的,但是这样一来,您就不能通过类调用该方法,只能通过转换为接口来调用。
I think your code is messed up from renaming, or else has some code in it that wasn't included in the question (DFResults, IDrilldown). I'm going to assume DFResults was supposed to be MyClass and IDrilldown was supposed to be Iface.
So you have an interface IFace, which declares 2 methods. You have a class that implements the interface, and EXPLICITLY implements that interface. You seem to accept that it must be explicit, but with that, you cannot call the method through the class, only by casting to the interface.