コード例 #1
0
        public void CanBindNewInstance()
        {
            const string defaultId = "default";
            const string childId   = "child";

            Injector injector = new Injector();

            injector.BindNewInstance <TestEmptyClass>();
            injector.BindNewInstance <TestEmptyClass, TestEmptyChildClass>();

            Assert.IsTrue(injector.ContainsCustomId <TestEmptyClass>(defaultId));
            Assert.IsTrue(injector.ContainsCustomId <TestEmptyClass>(childId));

            TestEmptyClass childClass = injector.Get <TestEmptyClass>(childId);

            Assert.AreEqual(typeof(TestEmptyChildClass), childClass.GetType());
        }
コード例 #2
0
        public static void InstancedInstantiation()
        {
            const string legendId = "legend";

            Injector injector = new Injector();

            injector.BindNewInstance <Weapon, HeroDefaultWeapon>();
            injector.BindNewInstance <Armor, HeroDefaultArmor>();
            injector.BindNewInstance <Character, Hero>();

            Weapon excalibur = new Weapon(8);

            injector.Bind(excalibur, "excalibur");

            Armor genjiArmor = new Armor(8);

            injector.Bind(genjiArmor, "genjiArmor");

            injector.Bind(new Hero(excalibur, genjiArmor), legendId);

            Hero legend = injector.Get <Hero>(legendId);

            Console.WriteLine(string.Format("This legend has weapon with atk = {0} and armor with def = {1}.", legend.Weapon.Atk, legend.Armor.Def));
        }