public void IterateOverDynamicPropertiesTest() { var ex = new ExpandoInstance(); ex.Name = "Rick"; ex.Entered = DateTime.Now; dynamic exd = ex; exd.Company = "West Wind"; exd.Accesses = 10; // Dictionary pseudo implementation ex["Count"] = 10; ex["Type"] = "NEWAPP"; // Dictionary Count - 2 dynamic props added Assert.IsTrue(ex.Properties.Count == 4); // iterate over all properties foreach (KeyValuePair <string, object> prop in exd.GetProperties(true)) { Console.WriteLine(prop.Key + " " + prop.Value); } }
public void XmlSerializeTest() { var ex = new ExpandoInstance(); ex.Name = "Rick"; ex.Entered = DateTime.Now; string address = "32 Kaiea"; ex["Address"] = address; ex["Contacted"] = true; dynamic exd = ex; exd.Count = 10; exd.Completed = DateTime.Now.AddHours(2); string xml; SerializationUtils.SerializeObject(ex, out xml); Console.WriteLine(xml); ExpandoInstance ex2 = SerializationUtils.DeSerializeObject(xml, typeof(ExpandoInstance)) as ExpandoInstance; Assert.IsNotNull(ex2); Assert.IsTrue(ex2["Address"] as string == address); }
public void JsonSerializeTest() { var ex = new ExpandoInstance(); ex.Name = "Rick"; ex.Entered = DateTime.Now; string address = "32 Kaiea"; ex["Address"] = address; ex["Contacted"] = true; dynamic exd = ex; exd.Count = 10; exd.Completed = DateTime.Now.AddHours(2); JavaScriptSerializer ser = new JavaScriptSerializer(); Console.WriteLine("*** Serialized Native object:"); Console.WriteLine(ser.Serialize(ex)); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("*** Serialized Dynamic object:"); Console.WriteLine(ser.Serialize(exd)); Console.WriteLine(); }
public void PropertyAsIndexerTest() { // Set standard properties var ex = new ExpandoInstance(); ex.Name = "Rick"; ex.Entered = DateTime.Now; Assert.AreEqual(ex.Name, ex["Name"]); Assert.AreEqual(ex.Entered, ex["Entered"]); }
public void DynamicAccessToPropertyTest() { // Set standard properties var ex = new ExpandoInstance(); ex.Name = "Rick"; ex.Entered = DateTime.Now; // turn into dynamic dynamic exd = ex; // Dynamic can access Assert.AreEqual(ex.Name, exd.Name); Assert.AreEqual(ex.Entered, exd.Entered); }
public void MixInObjectInstanceTest() { // Create expando an mix-in second objectInstanceTest var ex = new ExpandoInstance(new Address()); ex.Name = "Rick"; ex.Entered = DateTime.Now; // create dynamic dynamic exd = ex; // values should show Addresses initialized values (not null) Console.WriteLine(exd.FullAddress); Console.WriteLine(exd.Email); Console.WriteLine(exd.Phone); }
public void AddAndReadDynamicIndexersTest() { var ex = new ExpandoInstance(); ex.Name = "Rick"; ex.Entered = DateTime.Now; string address = "32 Kaiea"; ex["Address"] = address; ex["Contacted"] = true; dynamic exd = ex; Assert.AreEqual(exd.Address, ex["Address"]); Assert.AreEqual(exd.Contacted, true); }
public void AddAndReadDynamicPropertiesTest() { // strong typing first var ex = new ExpandoInstance(); ex.Name = "Rick"; ex.Entered = DateTime.Now; // create dynamic and create new props dynamic exd = ex; string company = "West Wind"; int count = 10; exd.Company = company; exd.Accesses = count; Assert.AreEqual(exd.Company, company); Assert.AreEqual(exd.Accesses, count); }
public void ExandoModesTest() { // Set standard properties var ex = new ExpandoInstance(); ex.Name = "Rick"; ex.Entered = DateTime.Now; // set dynamic properties dynamic exd = ex; 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 }
public void IterateOverDynamicPropertiesTest() { var ex = new ExpandoInstance(); ex.Name = "Rick"; ex.Entered = DateTime.Now; dynamic exd = ex; exd.Company = "West Wind"; exd.Accesses = 10; // Dictionary pseudo implementation ex["Count"] = 10; ex["Type"] = "NEWAPP"; // Dictionary Count - 2 dynamic props added Assert.IsTrue(ex.Properties.Count == 4); // iterate over all properties foreach (KeyValuePair<string, object> prop in exd.GetProperties(true)) { Console.WriteLine(prop.Key + " " + prop.Value); } }