帮助我理解 Validator.TryValidateObject() 方法

发布于 2024-09-03 20:12:56 字数 325 浏览 2 评论 0原文

这是方法定义:)

public static bool TryValidateObject(
Object instance,
ValidationContext validationContext,
ICollection<ValidationResult> validationResults,
bool validateAllProperties

感到困惑的是 validateAllProperties 参数,我知道它何时为 true-验证所有属性。

如果为 false 时,不验证所有属性,而是验证哪个属性呢?

this is the method definition:

public static bool TryValidateObject(
Object instance,
ValidationContext validationContext,
ICollection<ValidationResult> validationResults,
bool validateAllProperties

)

what i am confused is the validateAllProperties parameter, I understand when it is true-validate all properties.

What about when it is false, not validate all properties, but which property will be validated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉栀 2024-09-10 20:12:57

请参阅此处以获得良好的答案:

http://connect.microsoft.com/VisualStudio/feedback/details/605635/missleading-parametername-validateallproperties-in-validator-try-validate-componentemodel-dataannotations

似乎当设置 validateAllProperties 时设置为 false 表示仅验证RequiredAttribute。

See here for a good answer:

http://connect.microsoft.com/VisualStudio/feedback/details/605635/missleading-parametername-validateallproperties-in-validator-try-validate-componentemodel-dataannotations

It seems that when validateAllProperties is set to false that only the RequiredAttribute is validated.

一杯敬自由 2024-09-10 20:12:57

当属性为 false 时,Validator 应验证对象上应用了 ValidationAttribute 的每个属性。这可以包括以下任何属性:CustomValidationAttribute、DataTypeAttribute、RangeAttribute、RegularExpressionAttribute、RequiredAttribute 和 StringLengthAttribute,以及从 ValidationAttribute 派生的任何其他属性。

请参阅有关 TryValidateObject 方法的 MSDN 库<​​/a>了解更多信息。

在以下示例中,应验证 Foo,而不应验证 Bar。

public class Example
{
  [Required(ErrorMessage = "Foo is a required property.")]
  public object Foo { get; set; }

  public object Bar { get; set; }
}

When the property is false the Validator should validate each of the properties on the object that have a ValidationAttribute applied to them. This can include any of these attributes: CustomValidationAttribute, DataTypeAttribute, RangeAttribute, RegularExpressionAttribute, RequiredAttribute, and StringLengthAttribute, along with any other attributes that derive from ValidationAttribute.

See the MSDN library on the TryValidateObject method for more information.

In the following example, Foo should be validated, while Bar should not.

public class Example
{
  [Required(ErrorMessage = "Foo is a required property.")]
  public object Foo { get; set; }

  public object Bar { get; set; }
}
忆伤 2024-09-10 20:12:57

我也不完全理解它,但在与我编写的单元测试自定义验证器进行斗争之后,我注意到一件有趣的事情。

当我在没有此参数的情况下启动测试时(因此默认情况下它是 false),我的自定义验证器被省略了!如果我将其设置为 true,它们就会在我的测试中考虑在内,现在我可以愉快地继续 TDD。希望这对您有一点帮助。

I also don't fully understand it but after struggling with Unit Testing custom validators written by me I noticed one interresting thing.

When I launched my tests without this parameter (so by default it was false), my custom validators were omitted! if I set it to true, they were taken into account in my tests and now I can happily continue TDD. Hope this helps you a bit.

忱杏 2024-09-10 20:12:57

Arjen 是对的,当 validateAllProperties 参数为 false 时,仅验证必需属性。

我写了一篇关于使用 DataAnnotations 进行 OData 验证的文章,我发现了同样的问题。

http://blog.jorgef.net/2011/01/odata-dataannotations.html

Arjen is right, only the Required attribute is validated when the validateAllProperties parameter is false.

I wrote a post about OData validation using DataAnnotations and I found the same issue.

http://blog.jorgef.net/2011/01/odata-dataannotations.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文