简体   繁体   中英

c# ambiguous invocation implement and explict interface implementation

I have a question. I have a class and interface for it, so in class I have 3 methods that looks similar and those are:

    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
    }

And interface looks like:

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

    CurrentsFlagAnalysis GetCurrentsFlag(DateTime startDateTime, DateTime endDateTime);
}

The idea is to use those two methods with same name and same parameters but different return type similar to overload, but i came across the problem when i call this method for example:

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

From Compile time:

error CS1501: No overload for method 'GetCurrentsFlag' takes 3 arguments

and Visual studio sends me a message of ambiguous invocation and possible argument null exception;

I am getting ambiguous invocation implement error, I know i should use some sort of explicit implementation, but don't know hot to bite it.

And Another thing is this thing safe, should I just rename method and forget that idea.

Even from within the same class, once you've made methods be explicit interface implementations, you have to call them via a reference to the interface:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM