예제 #1
0
        public void TestCollections()
        {
            IMutablePicoContainer mpc = new DefaultPicoContainer();

            IParameter[] parameters = new IParameter[]
            {
                new ComponentParameter(typeof(Cod), false),
                new ComponentParameter(typeof(Fish), false)
            };

            mpc.RegisterComponentImplementation(typeof(CollectedBowl), typeof(CollectedBowl), parameters);
            mpc.RegisterComponentImplementation(typeof(Cod));
            mpc.RegisterComponentImplementation(typeof(Shark));
            Cod           cod  = (Cod)mpc.GetComponentInstanceOfType(typeof(Cod));
            CollectedBowl bowl = (CollectedBowl)mpc.GetComponentInstance(typeof(CollectedBowl));

            Assert.AreEqual(1, bowl.cods.Length);
            Assert.AreEqual(2, bowl.fishes.Length);
            Assert.AreSame(cod, bowl.cods[0]);

            try
            {
                Assert.AreSame(bowl.fishes[0], bowl.fishes[1]);
                Assert.Fail("The fishes should not be the same");
            }
            catch (AssertionException)
            {
            }
        }
예제 #2
0
        public void BowlWithoutTom()
        {
            IMutablePicoContainer mpc = new DefaultPicoContainer();

            mpc.RegisterComponentImplementation("Tom", typeof(Cod));
            mpc.RegisterComponentImplementation("Dick", typeof(Cod));
            mpc.RegisterComponentImplementation("Harry", typeof(Cod));
            mpc.RegisterComponentImplementation(typeof(Shark));

            IParameter[] parameters = new IParameter[]
            {
                new SampleCollectionComponentParameter(typeof(Cod), false),
                new CollectionComponentParameter(typeof(Fish), false)
            };

            mpc.RegisterComponentImplementation(typeof(CollectedBowl), typeof(CollectedBowl), parameters);

            CollectedBowl bowl = (CollectedBowl)mpc.GetComponentInstance(typeof(CollectedBowl));
            Cod           tom  = (Cod)mpc.GetComponentInstance("Tom");

            Assert.AreEqual(4, bowl.fishes.Length);
            Assert.AreEqual(2, bowl.cods.Length);
            Assert.IsFalse(new ArrayList(bowl.cods).Contains(tom));
        }