Inheritance: System.Dynamic.DynamicObject
Exemple #1
0
        static void Main()
        {
            dynamic dyn;

            dyn = 100;
            Console.WriteLine(dyn.GetType());
            Console.WriteLine(dyn);

            dyn = "This is a string";
            Console.WriteLine(dyn.GetType());
            Console.WriteLine(dyn);

            //   var d= new Person() { FirstName = "Bugs", LastName = "Bunny" };

            dyn = new Person()
            {
                FirstName = "Bugs", LastName = "Bunny"
            };
            Console.WriteLine(dyn.GetType());
            Console.WriteLine($"{dyn.FirstName} {dyn.LastName}");

            dyn           = new WroxDynamicObject();
            dyn.FirstName = "Bugs";
            dyn.LastName  = "Bunny";
            Console.WriteLine(dyn.GetType());
            Console.WriteLine($"{dyn.FirstName} {dyn.LastName}");

            dyn.MiddleName = "Rabbit";
            Console.WriteLine(dyn.MiddleName);
            Console.WriteLine(dyn.GetType());
            Console.WriteLine($"{dyn.FirstName} {dyn.MiddleName} {dyn.LastName}");
            List <Person> friends = new List <Person>();

            friends.Add(new Person()
            {
                FirstName = "Daffy", LastName = "Duck"
            });
            friends.Add(new Person()
            {
                FirstName = "Porky", LastName = "Pig"
            });
            friends.Add(new Person()
            {
                FirstName = "Tweety", LastName = "Bird"
            });
            dyn.Friends = friends;

            foreach (Person friend in dyn.Friends)
            {
                Console.WriteLine($"{friend.FirstName} {friend.LastName}");
            }

            Func <DateTime, string> GetTomorrow = today => today.AddDays(1).ToString("d");

            dyn.GetTomorrowDate = GetTomorrow;
            Console.WriteLine("Tomorrow is {0}", dyn.GetTomorrowDate(DateTime.Now));

            DoExpando();
            Console.Read();
        }
        static void Main()
        {
            dynamic dyn;
            dyn = 100;
            WriteLine(dyn.GetType());
            WriteLine(dyn);

            dyn = "This is a string";
            WriteLine(dyn.GetType());
            WriteLine(dyn);

            dyn = new Person() { FirstName = "Bugs", LastName = "Bunny" };
            WriteLine(dyn.GetType());
            WriteLine($"{dyn.FirstName} {dyn.LastName}");

            dyn = new WroxDynamicObject();
            dyn.FirstName = "Bugs";
            dyn.LastName = "Bunny";
            WriteLine(dyn.GetType());
            WriteLine($"{dyn.FirstName} {dyn.LastName}");

            dyn.MiddleName = "Rabbit";
            WriteLine(dyn.MiddleName);
            WriteLine(dyn.GetType());
            WriteLine($"{dyn.FirstName} {dyn.MiddleName} {dyn.LastName}");
            List<Person> friends = new List<Person>();
            friends.Add(new Person() { FirstName = "Daffy", LastName = "Duck" });
            friends.Add(new Person() { FirstName = "Porky", LastName = "Pig" });
            friends.Add(new Person() { FirstName = "Tweety", LastName = "Bird" });
            dyn.Friends = friends;
            foreach (Person friend in dyn.Friends)
            {
                WriteLine($"{friend.FirstName} {friend.LastName}");
            }

            Func<DateTime, string> GetTomorrow = today => today.AddDays(1).ToString("d");
            dyn.GetTomorrowDate = GetTomorrow;
            WriteLine("Tomorrow is {0}", dyn.GetTomorrowDate(DateTime.Now));

            DoExpando();
            Read();
        }