使用 RIA 服务自定义数据注释

发布于 2024-08-31 07:04:24 字数 563 浏览 1 评论 0原文

如果您将 RIAServices 与 Custom ValidatorAttributes 一起使用,那么这不仅仅是一个问题,更是一个警告。

这是 Senario,我正在创建一个自定义 DataAnnotation,它将根据是否已设置一系列可能的其他属性来验证属性,例如;如果 Prop1 为 100,则 Prop2、Prop3 或 Prop4 不能为 0,必须设置其中一个。我也在使用 RIA 服务,因此我创建了 ValidatorAttribute 我的 .shared.cs 文件。在为助手 CannotBeZeroIf 类编写所有测试后,我开始向模型类添加属性。当然,这就是事情开始变得非常糟糕的时候。 RIA 服务在 CodeGen 期间开始抛出 NullReferenceException。

创建RiaClientFiles任务-> NullReferenceException

事实证明,这与我使用验证器的构造函数将值传递给类这一事实有关。通过切换到使用 ObjectInitialization 语法,一切都得到了修复。

答案似乎是在处理 RIA 服务和自定义数据注释验证器时使用 ObjectInitializer 语法。

This is a word of warning more than a question, if you are using RIAServices with Custom ValidatorAttributes.

Here is the Senario, I was creating a custom DataAnnotation that would validate a property based on whether or not a possible series of other properties had been set, such as; if Prop1 was 100 then Prop2, Prop3, or Prop4 could not be 0 one of them had to be set. I am also using RIA Services so I created the ValidatorAttribute my .shared.cs file. After writing all the tests for the helper CannotBeZeroIf class, I began to add the attributes to the Model Class. This is of course when thing started to go very wrong. RIA Services began to throw up during the CodeGen, with a NullReferenceException.

CreateRiaClientFilesTask -> NullReferenceException

It turns out to be linked to the fact that I was using the validator's constructor to pass in the values to the class. By switching to using ObjectInitialization syntax, everything was fixed.

The Answer appears to be use ObjectInitializer syntax when dealing with RIA Services and Custom DataAnnotation Validators.

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

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

发布评论

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

评论(1

鹿港巷口少年归 2024-09-07 07:04:24

答案似乎是在处理 RIA 服务和自定义数据注释验证器时使用 ObjectInitializer 语法(这比我想象的更容易!;-)

The Answer appears to be use ObjectInitializer syntax when dealing with RIA Services and Custom DataAnnotation Validators (that was easier than I thought! ;-)

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