如何让microsoft unity为给定的接口类型“构造”一个类的列表.
非常简单的例子:
List<IShippingCalculation> list = new List<IShippingCalculation>(); list.Add(new NewYorkShippingCalculation()); list.Add(new FloridaShippingCalculation()); list.Add(new AlaskShippingCalculation()); //Not What I want public void calcship(List<IShippingCalculation> list) { var info = new ShippingInfo(list); info.CalculateShippingAmount(State.Alaska) } //Somehow in unity,must i do this for all the concrete classes? //how does it know to give a list. Container.RegisterType<IShippingInfo,new AlaskaShippingCalculation()>();?? //What I want public void calcship(IShippingInfo info) { info.CalculateShippingAmount(State.Alaska) }
谢谢!
解决方法
如果您使用的是Unity 2,则可以使用ResolveAll< T>
Container.RegisterType<IShippingInfo,FloridaShippingCalculation>("Florida"); Container.RegisterType<IShippingInfo,NewYorkShippingCalculation>("NewYork"); Container.RegisterType<IShippingInfo,AlaskaShippingCalculation>("Alaska"); IEnumerable<IShippingInfo> infos = Container.ResolveAll<IShippingInfo>();