F
F
Finn the Boy2018-08-20 13:17:17
C++ / C#
Finn the Boy, 2018-08-20 13:17:17

Error while implementing Generic interface in C#. How to decide?

There is the following code:

public interface IRequest<out TResponse>{}

public interface IPagedRequest<out TResponse>: IRequest<IPagedList<TResponse>> { 
    int Page { get; set; } 
    int PageSize { get; set; }
}

public interface IRequestHandler<in TRequest, TResponse> where TRequest : class, IRequest<TResponse> {
    Task<TResponse> HandleAsync(TRequest request, CancellationToken cancellationToken = default(CancellationToken));
}

You need to add a decorator to handle requests with pagination. I do the following:
public class PagedRequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse> where TRequest : class, IPagedRequest<TResponse> {
    // ...
}

To which the compiler responds with the following error: "The type TRequest must be convertable to IRequest in order to use it as parameter TRequest in the generic interface IRequestHandler".

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question