继承和验证应用程序块的 ObjectCollectionValidator 的问题
如果之前有人问过这个问题,我很抱歉,我尝试了多种关键字组合来描述该问题,但无法在 Google 或 SO 上找到任何内容。
问题很简单:我有一个对象列表。我已使用 ObjectCollectionValidator
标记此列表,以便对对象进行验证。然而,列表中的某些对象不是 T 类型,而是某种派生类型。
这些派生类型具有其父类型中不存在的需要验证的属性。他们根本不会。我相信 VAB 在处理 ObjectCollectionValidator
时不会检查继承类型,但我认为这是一个缺陷,所以我希望我错了。
顺便说一句,我正在使用 EntLib 4.1(虽然我想要,但我无法切换到 EntLib 5.0)。
I'm sorry if this has been asked before, I tried many combinations of keywords to describe the issue but was unable to find anything on Google or SO.
The problem is simple: and I have a List of objects. I've marked this list with ObjectCollectionValidator
so that objects get validated. Some of the objects in the list aren't, however, of type T, but of some derived type.
Those derived types have properties not present on their parents that need to be validated. They simply won't. I believe that VAB isn't checking inherited types when dealing with ObjectCollectionValidator
, but that is a flaw in my opinion, so I'm hoping I'm wrong.
By the way, I'm using EntLib's 4.1 (and while I wanted, I cannot switch to EntLib 5.0).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所看到的是 VAB 4.1 的一个设计怪癖,该怪癖已在 VAB 5.0 中得到解决。在 VAB 5.0 中,当您使用 ObjectCollectionValidatorAttribute 修饰集合属性时,您可以省略实际类型,在这种情况下,验证器将按对象的实际类型验证对象。
如果您无法切换到 VAB 5.0,您唯一能做的就是编写自己的对象集合验证器。这其实并不难。只需查看 VAB 5.0 的
ObjectCollectionValidatorAttribute
和ObjectCollectionValidator
的来源即可。祝你好运。
What you're witnessing is a design quirk of VAB 4.1 that has been solved in VAB 5.0. In VAB 5.0, when you decorate a collection property with a
ObjectCollectionValidatorAttribute
, you can leave out the actual type and in that case, the validator will validate objects by their actual type.If you can't switch to VAB 5.0, the only thing you can do is write your own object collection validator. This isn't really difficult. Just look at the source of of the
ObjectCollectionValidatorAttribute
andObjectCollectionValidator
of VAB 5.0.Good luck.