简体   繁体   English

我可以使用DataAnnotations在.NET中自定义验证对象吗?

[英]Can I use DataAnnotations to custom validate an object in .NET?

Is it possible to use DataAnnotations on an object and then .. programmatically, test to see if that object is Valid or not? 是否可以在对象上使用DataAnnotations然后以编程方式测试该对象是否Valid

I'm not using MVC3 or ModelBinding or ASP.NET .. but a WinForms app. 我没有使用MVC3ModelBindingASP.NET ..而是一个WinForms应用程序。

Is this possible? 这可能吗?

eg. 例如。

if (foo.IsValid) ? Hi() : Bye();

and the IsValid somehow checks each property that has DataAnnotations on em, or something. 并且IsValid以某种方式检查每个在em上具有DataAnnotations属性。 ?? ??

您正在寻找Validator

我知道SLaks已经回答了我的问题,但这 Scott Allen 对解决方案的另一个很好的详细说明 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用 DataAnnotations 来验证集合属性吗? - Can I use DataAnnotations to validate a collection property? .Net-DataAnnotations-验证2个依赖的DateTime - .Net - DataAnnotations - Validate 2 dependent DateTime 为什么不能将DataAnnotations KeyAttribute用于复合键? - Why can't i use DataAnnotations KeyAttribute for composite keys? 为什么我不能将资源用作带有 DataAnnotations 的 ErrorMessage? - Why can't I use resources as ErrorMessage with DataAnnotations? 如何使用asp.net mvc中的DataAnnotations验证字符串长度? - How would I validate string length using DataAnnotations in asp.net mvc? 当 model 包含另一个类的 object 作为使用 DataAnnotations 的属性时,如何在控制台应用程序中验证 model in.Net Core 3.1? - How to validate model in .Net Core 3.1 in console app when the model contains another class's object as a property using DataAnnotations? 如何将DataAnnotations ErrorMessageResourceName与自定义资源解决方案一起使用 - How to use DataAnnotations ErrorMessageResourceName with custom Resource Solution 当我使用 DataAnnotations 和 DisplayName 时 LabelFor 不起作用 - ASP.NET MVC - LabelFor doesn't work when I use DataAnnotations and DisplayName - ASP.NET MVC Asp.net自定义验证。 您可以使用哪些语言? - Asp.net Custom Validate. What languages can you use? 我可以在 DataAnnotations.StringLength 中使用数字以外的其他属性参数吗? - Can I use other attribute arguments than numbers in DataAnnotations.StringLength?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM