How to get implementing type during Ninject dependency resolve?

前端 未结 1 1079
孤独总比滥情好
孤独总比滥情好 2021-01-26 14:48

I use Log4net for logging and I have a lot of objects with an ILog dependency. These dependencies are injected just as the others. I would like to stick to the Log4net logger na

相关标签:
1条回答
  • 2021-01-26 15:24

    this and that covers your questions but they're not exactly duplicates, so i'll repost this:

    Bind<ILog>().ToMethod(context =>
                 LogManager.GetLogger(context.Request.ParentContext.Plan.Type));
    

    so context.Request.ParentContext.Plan.Type is the type ILog is injected into. If you ever want to do IResolutionRoot.Get<ILog>() then there won't be a type to inject ILog into and so there won't be a ParentContext either. In that case you'll need the null check as in your previous solution:

    Bind<ILog>().ToMethod(context =>
                 LogManager.GetLogger(context.Request.ParentContext == null ?
                                      typeof(object) : 
                                      context.Request.ParentContext.Plan.Type));
    
    0 讨论(0)
提交回复
热议问题