I have a method that is supposed return an object implementing a generic interface. It takes in a parameter that determines which class to instantiate and returns the instantiated object.
public class PlayerRetriever
{
public static IPlayer<T> Retrieve(string SitePath)
{
if (SitePath == "Home") { return new Player1(); }
else { return new AnotherPlayer(); }
}
}
interface IPlayer<T>
{
void RunPlayer();
List<T> RetrievePlayersByMovie(string movie);
}
Both "Player1" and "AnotherPlayer" implement IPlayer.
Why does my method give me the "type or namespace 'T' could not be found" error under the "T" in my method type?
What is the correct way of writing a method where the return type is an object implementing a generic interface?
Tis? You need to either give it that information when you call it by making itpublic static IPlayer<T> Retrieve<T>(string SitePath)or you need to return something non-generic.Ttype you're using onPlayer1andAnotherPlayerconnected by a hierarchy?