开发者

Is it possible to use C# DataAnnotations with IOC containers?

开发者 https://www.devze.com 2023-04-03 23:36 出处:网络
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

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 acce开发者_如何学Goss an annotation from an instantiated class. Or maybe I'm thinking about this backwords and should use validationContext.ObjectInstance?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号