Unity Resolve Multiple Classes

前端 未结 2 1504
萌比男神i
萌比男神i 2021-02-14 16:34

How do I get microsoft unity to \'construct\' a list of classes for a given interface type.

Very Simple example:

  List list          


        
相关标签:
2条回答
  • 2021-02-14 17:16

    You do not need to have the container as parameter, register the concrete types with names as said above, then in the constructor add a array as parameter, IList or generic Enum does not work.

    public MyConstructor(IMyType[] myTypes)
    
    0 讨论(0)
  • 2021-02-14 17:18

    If you are using Unity 2 you can use ResolveAll<T>

    Container.RegisterType<IShippingInfo,FloridaShippingCalculation>("Florida");
    Container.RegisterType<IShippingInfo,NewYorkShippingCalculation>("NewYork");
    Container.RegisterType<IShippingInfo,AlaskaShippingCalculation>("Alaska");
    
    IEnumerable<IShippingInfo> infos = Container.ResolveAll<IShippingInfo>();
    

    You have to give a name to every registration because ResolveAll will only return named registrations.

    0 讨论(0)
提交回复
热议问题