You cannot overload a method only with a return value (general or not). In addition, it would be impossible to resolve calls Bar, since the object could implement both IAlpha, and IBetaso, therefore this is not possible with overload.
public class AlphaBeta : IAlpha, IBeta
{
string x {set;}
string y {set;}
}
AlphaBeta parkingLot = myFoo.Bar<AlphaBeta>();
Below will also not work, because methods differ only in return type
class Gar
{
public string Foo()
{
return "";
}
public int Foo()
{
return 0;
}
}
, . .
public class Foo
{
private readonly static Dictionary<Type, Command> factories =
new Dictionary<Type, Command>();
static Foo()
{
factories.Add(typeof(IAlpha), new AlphaCreationCommand());
factories.Add(typeof(IBeta), new BetaCreationCommand());
}
public T Bar<T>()
{
if (factories.ContainsKey(typeof(T)))
{
return (T) factories[typeof(T)].Execute();
}
throw new TypeNotSupportedException(typeof(T));
}
}
IAlpha alphaInstance = myFoo.Bar<IAlpha>();
IBeta betaInstance = myFoo.Bar<IBeta>();
Bar, ( ), out. , , 100% - , .
public void Bar<T>(out T returnValue)
{
if (factories.ContainsKey(typeof(T)))
{
returnValue = (T) factories[typeof(T)].Execute();
return;
}
throw new TypeNotSupportedException(typeof(T));
}
IAlpha alphaInstance;
IBeta betaInstance;
myFoo.Bar(out alphaInstance);
myFoo.Bar(out betaInstance);
Command, AlphaCreationCommand, BetaCreationCommand TypeNotSupportedException. .
Func , - Foo, - , .