예제 #1
0
        private static dynamic InhertiableHyperDictionary()
        {
            Console.WriteLine("Using HyperDictionary to show dictionary inheritance\n==================================================");
            var top = new HyperDictionary("top");
            top["eyes"] = "brown";
            top["hair"] = "pointy";

            var second = new HyperDictionary("second");
            second.InheritsFrom(top);
            second["hair"] = "straight";

            Console.WriteLine("top[\"eyes\"]:\t{0}", top["eyes"]);
            Console.WriteLine("top[\"hair\"]:\t{0}", top["hair"]);
            Console.WriteLine("second[\"eyes\"]:\t{0}", second["eyes"]);
            Console.WriteLine("second[\"hair\"]:\t{0}", second["hair"]);

            //Extends and removes using an IEnumerable<object> value
            top["things"] = new string[] { "first thing", "second thing" };

            var third = new HyperDictionary("third");
            third.InheritsFrom(second);
            third.RemoveProperty("hair");
            third.ExtendProperty("things", new object[] { 3, 4, 5 });

            //Output members of third - note the absence of "hair" member
            Console.Write("third members:\n");
            foreach (object o in third)
            {
                Console.WriteLine(o);
            }
            Console.WriteLine();

            // Output the extended list of items in "things",
            // some from top and some from third.
            // And notice: DIFFERENT DATA TYPES!
            Console.Write("third things:\t");
            var things = third["things"] as IEnumerable<object>;
            foreach (object thing in things)
            {
                Console.Write(" | " + thing.ToString());
            }
            Console.Write(" | ");
            Console.WriteLine();

            Console.WriteLine("Making sure TryGetProperty works...should have 'not set' as the next value...");
            object stomach;
            if (third.TryGetProperty("stomach", out stomach)) Console.WriteLine("third\tstomach:\t{0}", stomach);
            else Console.WriteLine("not set - third.stomach");

            Console.WriteLine();
            Pause();

            return third;
        }