public class Manager<T> where T: IBallGame
{
T GetManager()
{
//if T is ISoccer return new Soccer()
//if T is IFootball return new Football()
//This wont work. Why?
if (typeof(T) == typeof(ISoccer))
return new Soccer();
}
}
Interface ISoccer: IBallgame
{
}
class Soccer: ISoccer
{
}
Interface IFootball: IBallgame
{
}
class Football:IFootball
{
}
我已经检查过这个问题How do I make the return type of a method of a generic? . 还有什么比 Convert.ChangeType() 更优雅的吗?
当对类型有限制时,为什么不能返回 Soccer 或 Football 的实例?