Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Validator属性(或其他属性)验证结构_C#_.net_.net Core_Fluentvalidation - Fatal编程技术网

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);