简体   繁体   中英

how to declare a generic method with a different “generic” return type

I have a interface that looks like this

public interface IBlueFinApiClient
{
    Task<BlueFinApiResponse> DecryptAsync(BlueFinApiRequest request);
}

This method, when implemented, just goes to an endpoint with the request and posts in. I need to use this end point to post another request of a different type and the return from Task<> will be of a different type as well. So, I am trying to change this into a generic but I am kind stuck on how to define the return type in a generic format:

public interface IBlueFinApiClient<T> where T:class
{
    Task<What should happen here?> DecryptAsync(T request) 

I was kinda exploring the covariance route but I am not sure if that's the answer here.

You can specify multiple generic type parameters, for example:

public interface IBlueFinApiClient<TInput, TReturn>
{
    Task<TReturn> DecryptAsync(TInput request);
}

Alternatively, you could specify the return type as a generic method:

public interface IBlueFinApiClient<TInput>
{
    Task<TReturn> DecryptAsync<TReturn>(TInput request);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM