Unity framework DependencyAttribute only works for public properties?

前端 未结 8 1203
小鲜肉
小鲜肉 2021-02-14 17:31

I was trying to clean up some accessability stuff in my code, and inadvertently broke Unity dependency injection. After a while I realized that I marked some public properties t

8条回答
  •  死守一世寂寞
    2021-02-14 17:56

    Another solution is to use [InjectionMethod] on a method where you pass the dependency into the class.

    public class MyClass {
    private ILogger logger;
    
    [InjectionMethod]
    public void Init([Dependency] ILogger logger)
    {
        this.logger = logger;
    

    ...etc


    and calling it:

    container.BuildUp(instanceOfMyClass);
    

    which will call Init with the dependency from unity.

    didn´t quite solve the problem, I know...but

    :-) J

提交回复
热议问题