这是我正在使用的代码,它在实例化时出现构建错误。我不确定为什么没有看到我的 SpecialHandler 是 BaseHandler 类型,T 设置为 SpecialEntity
static class HandlerFactory
{
public static BaseHandler<BaseEntity> Create(string typeString)
{
throw new NotImplementedException();
}
public static BaseHandler<T> Create<T>(string typeString ) where T : BaseEntity {
if (typeString == "Special")
**return new SpecialHandler();** //THERE'S BUILD ERROR HERE EVEN THOUGH Special Handler is inherits from type BaseHandler<T>
else
return null;
}
}
public class BaseHandler<T> where T : BaseEntity
{
public T GetEntity()
{
throw new NotImplementedException();
}
}
public class SpecialHandler : BaseHandler<SpecialEntity> {}
public class BaseEntity{}
public class SpecialEntity : BaseEntity{}