我们正在使用MediatR
,我希望将IMediator.Send
包裹在TRequest
的某些内容中。{/ p>
public class RequestBase
{
public User User { get; set; }
}
public class RequestBaseImpl : RequestBase, IRequest<SomeResponse>
{
}
public class HandlerBase
{
private IMediator _mediator;
public TReturn Send<T, TReturn>(T request) where T : RequestBase, IRequest<TReturn>
{
request.User = new User();
return _mediator.Send(request);
}
}
问题是,通过这种实现,我必须在调用Send方法时完全键入TRequest / TResponse类型。
var response = Send(new RequestBaseImpl());
给出以下编译错误:
错误CS0411方法的类型参数 &#39; HandlerBase.Send(T)&#39;无法从使用中推断出来。尝试指定 明确地输入参数。
我怎样才能避免写作
var response = Send<RequestBaseImpl, SomeResponse>(new RequestBaseImpl());