c#ambiguous调用实现和explict接口实现

时间:2018-05-10 08:13:03

标签: c#

我有一个问题。我有一个类和接口,因此在类中我有3个看起来相似的方法,它们是:

    public CurrentsFlagAnalysis GetCurrentsFlag(DateTime startDateTime, DateTime endDateTime)
    {
        //some code
    }

    CurrentsFlagAnalysis ICurrentService<CurrentsFlagAnalysis>.GetCurrentsFlag(DateTime startDateTime, DateTime endDateTime, byte id)
    {
        //some code
    }

    List<CurrentsFlagAnalysis> ICurrentService<List<CurrentsFlagAnalysis>>.GetCurrentsFlag(DateTime startDateTime, DateTime endDateTime, byte id)
    {
        //some cone
    }

界面如下:

public interface ICurrentService <out TCurrent>
{
    TCurrent GetCurrentsFlag(DateTime startDateTime, DateTime endDateTime, byte id);

    CurrentsFlagAnalysis GetCurrentsFlag(DateTime startDateTime, DateTime endDateTime);
}

这个想法是使用这两个方法具有相同的名称和相同的参数,但不同的返回类型类似于重载,但我在调用此方法时遇到了问题:

    public Task<List<CurrentsFlagAnalysis>> GetCurrentsFlagAsync(DateTime startDateTime, DateTime endDateTime, byte id)
    {
        return Task.Run(() => GetCurrentsFlag(startDateTime, endDateTime, id));
    }

从编译时间:

  

错误CS1501:方法没有过载&#39; GetCurrentsFlag&#39;需要3个参数

并且Visual Studio向我发送了一个模糊调用和可能的参数null异常的消息;

我得到了模糊的调用实现错误,我知道我应该使用某种明确的实现,但是不知道如何热爱它。

另一件事是这件事是安全的,我应该重命名方法而忘记这个想法。

1 个答案:

答案 0 :(得分:2)

即使是在同一个类中,一旦你使方法成为显式接口实现,你必须通过对接口的引用来调用它们:

return Task.Run(() => ((ICurrentService<CurrentsFlagAnalysis>)this).GetCurrentsFlag(
                                                               startDateTime, 
                                                               endDateTime, 
                                                               id));