使用Validator属性(或替代)来验证结构

时间:2016-12-08 02:43:01

标签: c# .net .net-core fluentvalidation

我们正在使用"FluentValidation.AspNetCore": "6.4.0-beta3"Validate属性适用于某个类。我们如何将它与结构一起使用?编译器抱怨:

  

属性'Validator'在此声明类型上无效。它仅对'class,parameter'声明有效。 [netstandard1.6]   ValidatorAttribute.ValidatorAttribute(Type validatorType)

[Validator(typeof(FoobarValidator))]
public struct FoobarDto
{
    public string Foo { get; set; }

    public string Bar { get; set; }
}

如果无法在结构上使用Validate属性,那么对结构使用流畅验证的替代方法是什么?

1 个答案:

答案 0 :(得分:1)

我想编译器很清楚你不能在结构上使用这个注释。您可以“手动”进行验证:

FoobarDto fooBar = new FoobarDto();
FoobarValidator validator = new FoobarValidator();
ValidationResult results = validator.Validate(fooBar);

或者将您的结构转换为类。

相关问题