从
new()
约束中删除无参数构造函数TValidator
的约束。
从 DI 获得
IStringLocalizer<ErrorResource>
服务。
用
Activator.CreateInstance()
创建TValidator
实例并提供Loc
作为参数。
public class FluentValidator<TValidator> : ComponentBase
where TValidator : IValidator
{
private readonly static char[] separators = new[] { '.', '[' };
private TValidator? validator;
[CascadingParameter] private EditContext? EditContext { get; set; }
[Inject]
IStringLocalizer<ErrorResource> Loc { get; set; }
protected override void OnInitialized()
{
if (EditContext != null)
{
validator = (TValidator)Activator.CreateInstance(typeof(TValidator), new object[] { Loc });
...
}
}
...
}
演示