一周以来,我收到以下类型的不匹配错误。我在互联网上搜索,查看了如何使用泛型,但找不到我做错的事情。谁能告诉我如何解决这个问题
static void Main(string[] args) {
JSonHttpClient httpClient;
// ....
public ListAlertsResponse ListAlerts(ListAlertsRequest listAlertsRequest) {
//HERE COUSES THE ERROR !!!
return (ListAlertsResponse)httpClient.DoGetRequest(listAlertsRequest);
}
}
错误:
Error 5 Argument 1:
cannot convert from 'ListAlertsRequest' to BaseRequest<BaseResponse>'
我的课程和介面
public class JsonHttpClient {
public BaseResponse DoGetRequest(BaseRequest<BaseResponse> request) {
return new BaseResponse(...) }
}
public interface Request {}
public interface Response {}
public abstract class BaseResponse : Response {}
public abstract class BaseRequest<T> : Request where T : BaseResponse {}
public class ListAlertsResponse : BaseResponse {}
public class ListAlertsRequest : BaseRequest<ListAlertsResponse> {}
最佳答案
listAlertsRequest不是BaseRequest的子类
public class JsonHttpClient
{
public BaseResponse DoGetRequest<T>(BaseRequest<T> request) where T : BaseResponse
{
return new BaseResponse(...)
}
}
关于c# - 使用泛型和多态性,会出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20807194/