У меня есть такая структураэкземпляр универсального типа в родовом методе
void Main()
{
var lol = ActionClass.GetTestTuple<Request, Response, RequestCallInfo>("lol", "user");
lol.Dump();
}
public class ActionClass
{
public static Tuple<TRes, TInfo> GetTestTuple<TReq, TRes, TInfo>(string resultMsg, string userName)
where TReq : BaseRequest, new()
where TRes : BaseResponse, new()
where TInfo : CallInfo<TReq>, new()
{
var response = new TRes { Message = resultMsg };
var eventsInfo = new TInfo();
eventsInfo.Data.UserName = userName;
return new Tuple<TRes, TInfo>(response, eventsInfo);
}
}
public class Request : BaseRequest
{
}
public class Response : BaseResponse
{
}
public class RequestCallInfo : CallInfo<Request>
{
public string Item { get; set; }
}
public class CallInfo<GenericType> : BaseCallInfo where GenericType : BaseRequest, new()
{
public GenericType Data { get; set; }
public CallInfo(GenericType x)
{
Data = x;
}
}
public class BaseCallInfo
{
public string CallItem { get; set; }
}
public class BaseRequest
{
public string UserName { get; set; }
}
public class BaseResponse
{
public string Message { get; set; }
}
и когда я исполню его, я получаю UserQuery.CallInfo<UserQuery.Request> does not contain a constructor that takes 0 arguments,
Так я попробовал этот способ
public static Tuple<TRes, TInfo> GetTestTuple<TReq, TRes, TInfo>(string resultMsg, string userName)
where TReq : BaseRequest, new()
where TRes : BaseResponse, new()
where TInfo : CallInfo<TReq>, new()
{
var response = new TRes { Message = resultMsg };
var eventsInfo = new TInfo(new TReq { UserName = userName });
eventsInfo.Data.UserName = userName;
return new Tuple<TRes, TInfo>(response, eventsInfo);
}
, но я получение 'TInfo': impossible to provide arguments when instantiating a type
Как сдать экземпляр TReq
- TInfo
и сохраните только конструктор параметров в CallInfo<GenericType>
?
OT: подписные ограничения в спецификации для C# 7. я действительно надеюсь, что они больше не откажут это. – Dbl