Inheritance: Westwind.Utilities.Expando
Ejemplo 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());
            }
        }
Ejemplo n.º 2
0
        public void TwoWayXmlSerializeExpandoTyped()
        {
            // Set standard properties
            var ex = new User();
            ex.Name = "Rick";
            ex.Active = true;

            // set dynamic properties
            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;

            // Serialize creates both static and dynamic properties
            // dynamic properties are serialized as a 'collection'
            string xml;
            SerializationUtils.SerializeObject(exd, out xml);
            Console.WriteLine("*** Serialized Dynamic object:");
            Console.WriteLine(xml);

            Assert.IsTrue(xml.Contains("Name")); // static
            Assert.IsTrue(xml.Contains("Company")); // dynamic

            // Serialize
            var user2 = SerializationUtils.DeSerializeObject(xml,typeof(User));
            SerializationUtils.SerializeObject(exd, out xml);
            Console.WriteLine(xml);

            Assert.IsTrue(xml.Contains("Rick")); // static
            Assert.IsTrue(xml.Contains("West Wind")); // dynamic
        }
Ejemplo n.º 3
0
        public void TwoWayJsonSerializeExpandoTyped()
        {
            // Set standard properties
            var ex = new User()
            {
                Name = "Rick",
                Email = "*****@*****.**",
                Password = "******",
                Active = true
            };

            // set dynamic properties
            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;

            // *** Should serialize both static properties dynamic properties
            var json = JsonConvert.SerializeObject(ex, Formatting.Indented);
            Console.WriteLine("*** Serialized Native object:");
            Console.WriteLine(json);

            Assert.IsTrue(json.Contains("Name")); // static
            Assert.IsTrue(json.Contains("Company")); // dynamic

            // *** Now deserialize the JSON back into object to
            // *** check for two-way serialization
            var user2 = JsonConvert.DeserializeObject<User>(json);
            json = JsonConvert.SerializeObject(user2, Formatting.Indented);
            Console.WriteLine("*** De-Serialized User object:");
            Console.WriteLine(json);

            Assert.IsTrue(json.Contains("Name")); // static
            Assert.IsTrue(json.Contains("Company")); // dynamic
        }
Ejemplo n.º 4
0
        public void InvalidAssignmentErrorOnStaticProperty()
        {
            dynamic dynUser = new User();
            dynUser.Name = 100;  // RuntimeBinderException

            // this should never run
            var user = dynUser as User;
            user.Name = "Rick";
            Console.WriteLine(user.Name);
            Console.WriteLine(user["Name"]);

            Assert.Fail("Invalid Assignment should have thrown exception");
            //>> 100
        }
Ejemplo n.º 5
0
        public void ExpandoMixinTest()
        {
            // have Expando work on Addresses
            var user = new User(new Address());

            // cast to dynamicAccessToPropertyTest
            dynamic duser = user;

            // Set strongly typed properties
            duser.Email = "*****@*****.**";
            user.Password = "******";

            // Set properties on address object
            duser.Address = "32 Kaiea";
            //duser.Phone = "808-123-2131";

            // set dynamic properties
            duser.NonExistantProperty = "This works too";

            // shows default value Address.Phone value
            Console.WriteLine(duser.Phone);
        }
Ejemplo n.º 6
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
        }
Ejemplo n.º 7
0
        public void AddAndReadDynamicPropertiesTest()
        {
            // strong typing first
            var ex = new User();
            ex.Name = "Rick";
            ex.Email = "*****@*****.**";

            // create dynamic and create new props
            dynamic exd = ex;

            string company = "West Wind";
            int count = 10;

            exd.entered = DateTime.Now;
            exd.Company = company;
            exd.Accesses = count;

            Assert.AreEqual(exd.Company, company);
            Assert.AreEqual(exd.Accesses, count);
        }