简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM