Injecting arrays with Unity

后端 未结 3 1570
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 20:19

My goal is to constructor inject an array of objects implementing an interface.

The following is the way I currently have it.

Container

        .Reg         


        
3条回答
  •  天涯浪人
    2020-12-17 21:13

    Using Resolve during "configuration time" is acceptable and often useful, and it's perfectly valid for arrays or enumerables.

    You could also have done the above by registering the ILoader[] type and registering each of the specific ILoader types using the RegisterType overload that takes a name.

    Then, wherever ILoader[] is required (e.g. needs to be injected), all of the above will resolve for you after configuration time. Of course, if you need multiple/different ILoader[] it would devolve back to needing to use Resolve during configuration time.

提交回复
热议问题