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

发布于 2024-12-17 21:00:08 字数 314 浏览 1 评论 0原文

是否可以在对象上使用 DataAnnotations,然后以编程方式测试该对象是否有效?

我没有使用 MVC3ModelBindingASP.NET ..而是使用 WinForms 应用程序。

这可能吗?

例如。

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

IsValid 以某种方式检查每个具有 DataAnnotations 的属性,或者其他东西。 ??

Is it possible to use DataAnnotations on an object and then .. programmatically, test to see if that object is Valid or not?

I'm not using MVC3 or ModelBinding or ASP.NET .. but a WinForms app.

Is this possible?

eg.

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

and the IsValid somehow checks each property that has DataAnnotations on em, or something. ??

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

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

发布评论

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

评论(2

李白 2024-12-24 21:00:08

您正在寻找验证器

You're looking for the Validator class.

是你 2024-12-24 21:00:08

我知道 SLAks 已经回答了我的问题,但是

I know SLaks has answered my question, but here is another good, detailed description of the solution from Scott Allen :)

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