使用 RIA 服务自定义数据注释
如果您将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案似乎是在处理 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! ;-)