Пример #1
0
        public void ExandoBasicTests()
        {
            // Set standard properties
            var ex = new User
            {
                Name   = "Name",
                Email  = "*****@*****.**",
                Active = true
            };

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

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

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

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

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

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

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

            // dynamic can access everything
            Expect(ex.Name, EqualTo(exd.Name));                              // native property
            Expect(ex["TotalOrderAmounts"], EqualTo(exd.TotalOrderAmounts)); // dictionary property
        }
Пример #2
0
        public void UserExampleTest()
        {
            // Set strongly typed properties
            var user = new User
            {
                Email    = "*****@*****.**",
                Password = "******",
                Name     = "Name1",
                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"] = "NickName1";
            duser["WebSite"] = "http://www.example.com";

            // 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))
            {
                var val = prop.Value;
                if (val == null)
                {
                    val = "null";
                }

                Trace.WriteLine(prop.Key + ": " + val);
            }
        }
Пример #3
0
        public void UserExampleTest()
        {
            // Set strongly typed properties
            var user = new User
            {
                Email = "*****@*****.**",
                Password = "******",
                Name = "Name1",
                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"] = "NickName1";
            duser["WebSite"] = "http://www.example.com";

            // 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))
            {
                var val = prop.Value;
                if (val == null)
                    val = "null";

                Trace.WriteLine(prop.Key + ": " + val);
            }
        }
Пример #4
0
        public void ExandoBasicTests()
        {
            // Set standard properties
            var ex = new User
            {
                Name = "Name",
                Email = "*****@*****.**",
                Active = true
            };

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

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

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

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

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

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

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