Inheritance: System.Dynamic.DynamicObject, IDynamicMetaObjectProvider
        public void ExpandoToAndFromXml()
        {            
            dynamic properties = new Expando();            
            properties.Item1 = "Item 1 text";
            properties.Item2 = "Item 2 text";
            properties.Number3 = 10.22;


            string xml = properties.Properties.ToXml();

            Assert.IsNotNull(xml);
            Console.WriteLine(xml);

            dynamic properties2 = new Expando();
            Expando expando = properties2 as Expando;

            properties2.Properties.FromXml(xml);
            
            Assert.IsTrue(properties2.Item1 == properties.Item1);
            Assert.IsTrue(properties2.Number3 == properties.Number3);

            Console.WriteLine(properties2.Item1 + " " + 
                              properties2.Number3 + " " + 
                              properties["Item2"]);
            
        }
Beispiel #2
0
        /// <summary>
        /// Create an Expando from a dictionary
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="expando">Expando instance</param>
        public Expando(IDictionary <string, object> dict)
        {
            var expando = this;

            Initialize(expando);

            Properties = new PropertyBag();

            foreach (var kvp in dict)
            {
                var kvpValue = kvp.Value;
                if (kvpValue is IDictionary <string, object> )
                {
                    var expandoVal = new Expando(kvpValue);
                    expando[kvp.Key] = expandoVal;
                }
                else if (kvp.Value is ICollection)
                {
                    // iterate through the collection and convert any string-object dictionaries
                    // along the way into expando objects
                    var objList = new List <object>();
                    foreach (var item in (ICollection)kvp.Value)
                    {
                        var itemVals = item as IDictionary <string, object>;
                        if (itemVals != null)
                        {
                            var expandoItem = new Expando(itemVals);
                            objList.Add(expandoItem);
                        }
                        else
                        {
                            objList.Add(item);
                        }
                    }
                    expando[kvp.Key] = objList;
                }
                else
                {
                    expando[kvp.Key] = kvpValue;
                }
            }
        }
        public void ExpandoFromDictionary()
        {
            var dict = new Dictionary<string, object>()
            {
                {"Name", "Rick"},
                {"Company", "West Wind"},
                {"Accesses", 2}
            };

            dynamic expando = new Expando(dict);

            Console.WriteLine(expando.Name);
            Console.WriteLine(expando.Company);
            Console.WriteLine(expando.Accesses);

            Assert.AreEqual(dict["Name"], expando.Name);
            Assert.AreEqual(dict["Company"], expando.Company);
            Assert.AreEqual(dict["Accesses"], expando.Accesses);
        }
Beispiel #4
0
        /// <summary>
        /// Create an Expando from a dictionary
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="expando">Expando instance</param>
        public Expando(IDictionary<string, object> dict)
        {
            var expando = this;

            Initialize(expando);

            Properties = new PropertyBag();

            foreach (var kvp in dict)
            {
                var kvpValue = kvp.Value;
                if (kvpValue is IDictionary<string,object>)
                {
                    var expandoVal = new Expando(kvpValue);
                    expando[kvp.Key] = expandoVal;
                }
                else if (kvp.Value is ICollection)
                {
                    // iterate through the collection and convert any string-object dictionaries
                    // along the way into expando objects
                    var objList = new List<object>();
                    foreach (var item in (ICollection)kvp.Value)
                    {
                        var itemVals = item as IDictionary<string, object>;
                        if (itemVals != null)
                        {
                            var expandoItem = new Expando(itemVals);
                            objList.Add(expandoItem);
                        }
                        else
                        {
                            objList.Add(item);
                        }
                    }
                    expando[kvp.Key] = objList;
                }
                else
                {
                    expando[kvp.Key] = kvpValue;
                }
            }
        }