コード例 #1
0
        public void Print()
        {
            // get the CoolData property from Comp2
            // we use the ObjectManager, so we don't need an explicit reference to Comp2
            CompTwo compTwo = (CompTwo)ObjectManager.Get("comp2");

            Console.WriteLine(compTwo.CoolData);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            // create and add components to the manager
            ObjectManager.Add("comp1", ObjectFactory.New("CompOne"));
            ObjectManager.Add("comp2", ObjectFactory.New("CompTwo"));
            ObjectManager.Add("comp3", ObjectFactory.New("CompThree"));

            // create a local CompOne. get an Object from the ObjectManger, cast it to CompOne
            // then call print, which is a method of CompOne
            CompOne c1 = (CompOne)ObjectManager.Get("comp1");

            c1.Print();

            // iterate over the list of identifiers in our objects list
            foreach (string obj in ObjectManager.ListIdentifiers())
            {
                // convert the object to IUpdateable
                // this allows us to call update on the object, because it is required by IUpdateable
                // notice that comp3 never checks in
                if (ObjectManager.Get(obj) is IUpdateable o)
                {
                    o.Update(true);
                }
            }

            // get two instances of Comp2
            // shows that mulitple calls to the Manager with a given identifier yield the same object
            CompTwo comp2_1 = (CompTwo)ObjectManager.Get("comp2");
            CompTwo comp2_2 = (CompTwo)ObjectManager.Get("comp2");

            // change the member data in once instance
            comp2_1.CoolData = "not so cool";

            // print the changed data in another instance
            comp2_2.Print();
        }