Я пытался отправить класс универсального типа через посредник.
Я создал класс CreateSettingCommand, который принимает любую модель через:
var createUserCommand = new CreateSettingCommand<Database.Models.Product> { Setting = Product };
var result = await mediator.Send(createUserCommand);
При запуске я получаю следующую ошибку:
ArgumentException: The number of generic arguments provided doesn't equal the arity of the generic type definition. (Parameter 'instantiation')
Это мой класс CreateSettingCommand:
public class CreateSettingCommand<T> : IRequest<CreateSettingCommandResponse>
{
public T Setting { get; set; }
}
public class CreateSettingCommandResponse : BaseResponse
{
}
public class CreateSettingCommandHandler<T> : IRequestHandler<CreateSettingCommand<T>, CreateSettingCommandResponse>
{
private readonly ApplicationDbContext context;
private readonly IHttpContextAccessor httpContext;
private readonly UserManager<ApplicationUser> userManager;
public CreateSettingCommandHandler(ApplicationDbContext context, IHttpContextAccessor httpContext, UserManager<ApplicationUser> userManager)
{
this.context = context;
this.httpContext = httpContext;
this.userManager = userManager;
}
public async Task<CreateSettingCommandResponse> Handle(CreateSettingCommand<T> request, CancellationToken cancellationToken)
{
var response = new CreateSettingCommandResponse();
var currentUser = await userManager.FindByNameAsync(httpContext.HttpContext.User.Identity.Name);
if (currentUser != null)
{
try
{
// set value to generic class
Type type = request.Setting.GetType();
type.GetProperty("ActionBy").SetValue(type, currentUser.UserName);
type.GetProperty("ActionOn").SetValue(type, DateTime.Now);
type.GetProperty("ApplicationUserId").SetValue(type, currentUser.Id);
await context.SaveChangesAsync();
}
catch (Exception ex)
{
response.AddError(ex.Message);
}
}
else
{
response.AddError("Current logged in user not found.");
}
return response;
}
}
Это мой запуск:
services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies());
services.AddScoped(typeof(IRequestHandler<,>), typeof(CreateSettingCommandHandler<>));
Любая помощь будет оценена!
p / s:это веб-приложение