C# 使用Validator属性(或其他属性)验证结构
我们使用的是C# 使用Validator属性(或其他属性)验证结构,c#,.net,.net-core,fluentvalidation,C#,.net,.net Core,Fluentvalidation,我们使用的是“FluentValidation.AspNetCore”:“6.4.0-beta3”。Validate属性适用于类。如何将其与结构一起使用?编译器抱怨: 属性“Validator”对此声明类型无效。它仅对“类,参数”声明有效。[网络标准1.6] ValidatorAttribute.ValidatorAttribute(类型validatorType) 如果无法在结构上使用Validate属性,那么对结构使用fluent验证的替代方法是什么 我想编译器很清楚,不能在结构上使用此注释
“FluentValidation.AspNetCore”:“6.4.0-beta3”
。Validate
属性适用于类。如何将其与结构一起使用?编译器抱怨:
属性“Validator”对此声明类型无效。它仅对“类,参数”声明有效。[网络标准1.6]
ValidatorAttribute.ValidatorAttribute(类型validatorType)
如果无法在结构上使用
Validate
属性,那么对结构使用fluent验证的替代方法是什么 我想编译器很清楚,不能在结构上使用此注释。您可以“手动”进行验证:
或者将结构转换为类
[Validator(typeof(FoobarValidator))]
public struct FoobarDto
{
public string Foo { get; set; }
public string Bar { get; set; }
}
FoobarDto fooBar = new FoobarDto();
FoobarValidator validator = new FoobarValidator();
ValidationResult results = validator.Validate(fooBar);