首页 文章

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

提问于
浏览
0

我有个问题 . 我有一个类和接口,所以在类中我有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:方法'GetCurrentsFlag'没有重载需要3个参数

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

我得到了模糊的调用工具错误,我知道我应该使用某种显式实现,但不知道热点咬它 .

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

1 回答

  • 2

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

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

相关问题