是否可以将 C# DataAnnotations 与 IOC 容器一起使用?
是否可以将 C# DataAnnotations 与 IOC 容器一起使用?我有一个 ValidationAttribute,我想在类实例化后将解析的对象注入到属性类中。基本上,我想从实例化的类访问注释。或者也许我正在考虑这个backwords并且应该使用validationContext.ObjectInstance?
Is it possible to use C# DataAnnotations with IOC containers? I've got a ValidationAttribute that I'd like to inject a resolved object into the attribute class after the class is instantiated. Basicly, I want to access an annotation from an instantiated class. Or maybe I'm thinking about this backwords and should use validationContext.ObjectInstance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有干净的方法来使用它(使用 DI)。但是,如果您创建静态方法来访问当前 IoC 容器,则可以使用 IoC。这非常令人讨厌,但这是我找到的唯一解决方案。
我所说的静态是指从应用程序或请求缓存中获取数据的静态访问器。
There's no clean way of using this (with DI). You can however use IoC if you create a static method to access the current IoC Container. It's pretty nasty but it's the only solution I found.
By static I mean static accessor that grabs it from the application or request cache.