コード例 #1
0
ファイル: ExpandoTests.cs プロジェクト: vip32/Xtricate
        public void TwoWayXmlSerializeExpandoTyped()
        {
            // Set standard properties
            var ex = new User {Name = "Name", Active = true};

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

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

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

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

            Assert.IsTrue(xml.Contains("Name")); // static
            Assert.IsTrue(xml.Contains("Company")); // dynamic
        }
コード例 #2
0
ファイル: ExpandoTests.cs プロジェクト: vip32/Xtricate
        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
ファイル: ExpandoTests.cs プロジェクト: vip32/Xtricate
        public void ToExandoBasicTests()
        {
            // Set standard properties
            var ex = new User
            {
                Name = "Name",
                Email = "*****@*****.**",
                Active = true
            };

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

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

            // You can access dynamic properties either as dynamic or via IDictionary
            Assert.AreEqual(ex.ToExpando().Company, ex["Company"] as string, "Company 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, ex.ToExpando().Name); // native property
        }
コード例 #4
0
ファイル: ExpandoTests.cs プロジェクト: vip32/Xtricate
        public void TwoWayJsonSerializeExpandoTyped()
        {
            // Set standard properties
            var ex = new User
            {
                Name = "Name",
                Email = "*****@*****.**",
                Password = "******",
                Active = true
            };

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

            // *** Should serialize both standard properties and dynamic properties
            var json = JsonConvert.SerializeObject(ex);
            Assert.IsTrue(!string.IsNullOrEmpty(json));
            Trace.WriteLine("*** Serialized Native object:");
            Trace.WriteLine(json);

            Assert.IsTrue(json.Contains("Name")); // standard
            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);
            Assert.IsNotNull(user2);
            json = JsonConvert.SerializeObject(user2);
            Assert.IsTrue(!string.IsNullOrEmpty(json));
            Trace.WriteLine("*** De-Serialized User2 object:");
            Trace.WriteLine(json);

            Assert.IsTrue(json.Contains("Name")); // standard
            Assert.IsTrue(json.Contains("Company")); // dynamic
        }
コード例 #5
0
ファイル: ExpandoTests.cs プロジェクト: vip32/Xtricate
        public void ExpandoMixinTest1()
        {
            // 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 = "Address 123";
            duser.Phone = "111 222 333 444";

            // set dynamic properties
            duser.NonExistantProperty = "NonExistantProperty1";

            // shows default value Address.Phone value
            Trace.WriteLine(string.Format("phone:{0}", duser.Phone as string));
            Assert.AreEqual(user["Phone"], "111 222 333 444");
            Assert.AreEqual(duser.Phone, "111 222 333 444");
        }
コード例 #6
0
ファイル: ExpandoTests.cs プロジェクト: vip32/Xtricate
        public void ExpandoInstanceCanAcceptExtensionMetods()
        {
            var user = new User
            {
                Email = "*****@*****.**",
                Password = "******",
                Name = "Name1",
                Active = true
            };

            dynamic duser = user;

            duser.Phone = "111 222 333 444";

            var json = JsonConvert.SerializeObject(user);
            Trace.WriteLine(json);
            Assert.IsTrue(!string.IsNullOrEmpty(json));

            Assert.Throws<RuntimeBinderException>(() => duser.Dump()); // does not contain a definition for 'Dump'
        }
コード例 #7
0
ファイル: ExpandoTests.cs プロジェクト: vip32/Xtricate
        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
        }
コード例 #8
0
ファイル: ExpandoTests.cs プロジェクト: vip32/Xtricate
        public void ChildObjectTest()
        {
            var user = new User();

            dynamic duser = user;

            // Set properties on dynamic object
            duser.Address = new Address();
            duser.Address.FullAddress = "Address 123";
            duser.Address.Phone = "111 222 333 444";

            Assert.AreEqual(duser.Address.Phone, "111 222 333 444");
        }
コード例 #9
0
ファイル: ExpandoTests.cs プロジェクト: vip32/Xtricate
        public void AddAndReadDynamicPropertiesTest()
        {
            // strong typing first
            var ex = new User
            {
                Name = "Name",
                Email = "*****@*****.**"
            };

            // create dynamic and create new props
            dynamic exd = ex;
            exd.entered = DateTime.Now;
            exd.Company = "Company";
            exd.Accesses = 10;

            Assert.AreEqual(exd.Company, "Company");
            Assert.AreEqual(exd.Accesses, 10);
        }