private void CollectGameByDIContainer() { var container = new StandardKernel(); container.Bind <Physics>().To <FourWays>(); container.Bind <GameObject>().To <BirdPlayer>().InSingletonScope(); container.Bind <GameObject>().To <Bush>(); container.Bind <Position>() .ToConstant(new Position(new Point(100, 100), new Size(50, 40))) .WhenInjectedInto <BirdPlayer>(); container.Bind <Position>() .ToConstant(new Position(new Point(10, 10), new Size(50, 50))) .WhenInjectedInto <Bush>(); // Код, который требуется повторить для всех // IConditionalEvent<T1, T2> where T1, T2 : GameObject: // world.AddIConditionalEvent(new Collect()); // Не работает из-за отсутствия ковариантности: /*container.Bind<IConditionalEvent<GameObject, GameObject>>().To<Collect>(); * container.Bind<World>().ToSelf() * .OnActivation(w => * { * foreach (var c in container * .GetAll<IConditionalEvent<GameObject, GameObject>>()) * w.AddIConditionalEvent(c); * }); */ // Единственный способ повторить автоматически - но он ничего не сокращает container.Bind <IConditionalEvent <BirdPlayer, Bush> >().To <Collect>(); container.Bind <World>().ToSelf() .OnActivation(w => { foreach (var c in container .GetAll <IConditionalEvent <BirdPlayer, Bush> >()) { w.AddIConditionalEvent(c); } }); world = container.Get <World>(); player = container.Get <BirdPlayer>(); var assembly = Assembly.GetExecutingAssembly(); world.CollectConditionalEventsAutomatically(assembly); }
private void CollectGameByHands() { player = new BirdPlayer(new Position(new Point(450, 200), new Size(60, 50))); var objects = new GameObject[] { player, new Bush(new Position(new Point(100, 100), new Size(10, 10))), new Bush(new Position(new Point(900, 100), new Size(10, 10))), new Bush(new Position(new Point(100, 300), new Size(10, 10))), new Bush(new Position(new Point(400, 500), new Size(10, 10))) }; world = new World(new FourWays(), objects); world.AddIConditionalEvent(new Collect()); }