Exemplo n.º 1
0
        public void UserExampleTest()
        {
            var user = new User();

            // Set strongly typed properties
            user.Email    = "*****@*****.**";
            user.Password = "******";
            user.Name     = "Rickochet";
            user.Active   = true;

            // Now add dynamic properties
            dynamic duser = user;

            duser.Entered  = DateTime.Now;
            duser.Accesses = 1;

            // you can also add dynamic props via indexer
            user["NickName"] = "Wreck";
            duser["WebSite"] = "http://www.west-wind.com/weblog";

            // Access strong type through dynamic ref
            Assert.AreEqual(user.Name, duser.Name);

            // Access strong type through indexer
            Assert.AreEqual(user.Password, user["Password"]);


            // access dyanmically added value through indexer
            Assert.AreEqual(duser.Entered, user["Entered"]);

            // access index added value through dynamic
            Assert.AreEqual(user["NickName"], duser.NickName);


            // loop through all properties dynamic AND strong type properties (true)
            foreach (var prop in user.GetProperties(true))
            {
                object val = prop.Value;
                if (val == null)
                {
                    val = "null";
                }

                Console.WriteLine(prop.Key + ": " + val.ToString());
            }
        }
Exemplo n.º 2
0
        public void ExandoBasicTests()
        {
            // Set standard properties
            var ex = new User()
            {
                Name   = "Rick",
                Email  = "*****@*****.**",
                Active = true
            };

            // set dynamic properties that don't exist on type
            dynamic exd = ex;

            exd.Entered  = DateTime.Now;
            exd.Company  = "West Wind";
            exd.Accesses = 10;

            // set dynamic properties as dictionary
            ex["Address"]           = "32 Kaiea";
            ex["Email"]             = "*****@*****.**";
            ex["TotalOrderAmounts"] = 51233.99M;

            // iterate over all properties dynamic and native
            foreach (var prop in ex.GetProperties(true))
            {
                Console.WriteLine(prop.Key + " " + prop.Value);
            }

            // you can access plain properties both as explicit or dynamic
            Assert.AreEqual(ex.Name, exd.Name, "Name doesn't match");

            // You can access dynamic properties either as dynamic or via IDictionary
            Assert.AreEqual(exd.Company, ex["Company"] as string, "Company doesn't match");
            Assert.AreEqual(exd.Address, ex["Address"] as string, "Name doesn't match");

            // You can access strong type properties via the collection as well (inefficient though)
            Assert.AreEqual(ex.Name, ex["Name"] as string);

            // dynamic can access everything
            Assert.AreEqual(ex.Name, exd.Name);                              // native property
            Assert.AreEqual(ex["TotalOrderAmounts"], exd.TotalOrderAmounts); // dictionary property
        }
Exemplo n.º 3
0
        public void UserExampleTest()
        {
            var user = new User();

            // Set strongly typed properties
            user.Email = "*****@*****.**";
            user.Password = "******";
            user.Name = "Rickochet";
            user.Active = true;

            // Now add dynamic properties
            dynamic duser = user;
            duser.Entered = DateTime.Now;
            duser.Accesses = 1;

            // you can also add dynamic props via indexer
            user["NickName"] = "Wreck";
            duser["WebSite"] = "http://www.west-wind.com/weblog";

            // Access strong type through dynamic ref
            Assert.AreEqual(user.Name, duser.Name);

            // Access strong type through indexer
            Assert.AreEqual(user.Password, user["Password"]);

            // access dyanmically added value through indexer
            Assert.AreEqual(duser.Entered, user["Entered"]);

            // access index added value through dynamic
            Assert.AreEqual(user["NickName"], duser.NickName);

            // loop through all properties dynamic AND strong type properties (true)
            foreach (var prop in user.GetProperties(true))
            {
                object val = prop.Value;
                if (val == null)
                    val = "null";

                Console.WriteLine(prop.Key + ": " + val.ToString());
            }
        }
Exemplo n.º 4
0
        public void ExandoBasicTests()
        {
            // Set standard properties
            var ex = new User()
            {
                Name = "Rick",
                Email = "*****@*****.**",
                Active = true
            };

            // set dynamic properties that don't exist on type
            dynamic exd = ex;
            exd.Entered = DateTime.Now;
            exd.Company = "West Wind";
            exd.Accesses = 10;

            // set dynamic properties as dictionary
            ex["Address"] = "32 Kaiea";
            ex["Email"] = "*****@*****.**";
            ex["TotalOrderAmounts"] = 51233.99M;

            // iterate over all properties dynamic and native
            foreach (var prop in ex.GetProperties(true))
            {
                Console.WriteLine(prop.Key + " " + prop.Value);
            }

            // you can access plain properties both as explicit or dynamic
            Assert.AreEqual(ex.Name, exd.Name, "Name doesn't match");

            // You can access dynamic properties either as dynamic or via IDictionary
            Assert.AreEqual(exd.Company, ex["Company"] as string, "Company doesn't match");
            Assert.AreEqual(exd.Address, ex["Address"] as string, "Name doesn't match");

            // You can access strong type properties via the collection as well (inefficient though)
            Assert.AreEqual(ex.Name, ex["Name"] as string);

            // dynamic can access everything
            Assert.AreEqual(ex.Name, exd.Name); // native property
            Assert.AreEqual(ex["TotalOrderAmounts"], exd.TotalOrderAmounts); // dictionary property
        }