Reputation: 46
I want to create generic method with generic parameter like that:
public class BaseResponse<T>
{
public ResponseReturnCode Code { get; set; }
public string Description { get; set; }
public T Data { get; set; }
}
public class LoginResponse : BaseResponse<LoginData>
private async Task<T> CreateResponse<T,U>(HttpResponseMessage responseMessage) where T : BaseResponse<U>
{
var responseBody = await responseMessage.Content.ReadAsStringAsync();
var responseDto = JsonConvert.DeserializeObject<T>(responseBody);
// Some additional stuff here but I am not need U type for that.
// I am only touching not generic part of BaseResponse here.
//...
return responseDto;
}
This is working:
var responseDto = await CreateResponse<LoginResponse, LoginData>(responseMessage);
private async Task<T> CreateResponse<T,U>(HttpResponseMessage responseMessage) where T : BaseResponse<U> {...}
I don't want to specify internal type "LoginData" when calling this method. But this statment dosn't work:
var responseDto = await CreateResponse<LoginResponse>(responseMessage);
private async Task<T> CreateResponse<T>(HttpResponseMessage responseMessage) where T : BaseResponse<U> where U : class
{...}
"U" type i unknown...
this also doesn't work:
var responseDto = await CreateResponse<LoginResponse>(responseMessage);
private async Task<T> CreateResponse<T>(HttpResponseMessage responseMessage) where T : BaseResponse<object>
{...}
In Java i can use wildcard but hot to solve this i c#?
Upvotes: 0
Views: 383