我们正在使用 "FluentValidation.AspNetCore": "6.4.0-beta3"
。 Validate
属性适用于类。我们如何将它与结构一起使用?编译器提示:
Attribute 'Validator' is not valid on this declaration type. It is only valid on 'class, parameter' declarations. [netstandard1.6] ValidatorAttribute.ValidatorAttribute(Type validatorType)
[Validator(typeof(FoobarValidator))]
public struct FoobarDto
{
public string Foo { get; set; }
public string Bar { get; set; }
}
如果无法在结构上使用 Validate
属性,那么对结构使用流畅验证的替代方法是什么?
最佳答案
我猜编译器很清楚你不能在结构上使用这个注解这一事实。您可以“手动”进行验证:
FoobarDto fooBar = new FoobarDto();
FoobarValidator validator = new FoobarValidator();
ValidationResult results = validator.Validate(fooBar);
或者将您的结构转换为一个类。
关于c# - 使用 Validator 属性(或替代属性)来验证结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41030905/