Resolving IEnumerable with Unity

前端 未结 8 1078
梦如初夏
梦如初夏 2020-12-04 22:05

Can Unity automatically resolve IEnumerable?

Let\'s say I have a class with this constructor:

public CoalescingParserSelector(I         


        
8条回答
  •  时光取名叫无心
    2020-12-04 22:31

    If you want to generally support IEnumerable, then you can add this line:

    _container.RegisterType(typeof(IEnumerable<>), new InjectionFactory((IUnityContainer container, Type type, string name) => container.ResolveAll(type.GetGenericArguments().Single())));
    

    This is he generic version of

    container.RegisterType, IParserBuilder[]>();
    

提交回复
热议问题