[英]c# ambiguous invocation implement and explict interface implementation
我有個問題。 我有一個類和接口,因此在類中,我有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異常;
我收到了模棱兩可的調用工具錯誤,我知道我應該使用某種顯式的實現,但是不知道該怎么做。
還有一件事情是安全的,我應該重命名方法而忘記那個想法。
即使在同一個類中,一旦使方法成為顯式的接口實現,就必須通過對接口的引用來調用它們:
return Task.Run(() => ((ICurrentService<CurrentsFlagAnalysis>)this).GetCurrentsFlag(
startDateTime,
endDateTime,
id));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.