Esempio n. 1
0
        /// <summary>
        ///     Create an Expando from a dictionary
        /// </summary>
        /// <param name="dict"></param>
        public Expando(IDictionary <string, object> dict)
        {
            var expando = this;

            this.Initialize(expando);

            this.Properties = new Dictionary <string, object>();
            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;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Converts an <see cref="System.Collections.IDictionary" /> into an <see cref="Expando" />
        /// </summary>
        /// <returns>
        ///     <see cref="Expando" />
        /// </returns>
        public static Expando ToIndexableExpando(IDictionary <string, object> dict)
        {
            var expando = new Expando();


            foreach (var kvp in dict)
            {
                var kvpValue = kvp.Value as IDictionary <string, object>;
                if (kvpValue != null)
                {
                    var expandoVal = ToIndexableExpando(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 = ToIndexableExpando(itemVals);
                            objList.Add(expandoItem);
                        }
                        else
                        {
                            objList.Add(item);
                        }
                    }

                    expando[kvp.Key] = objList;
                }
                else
                {
                    expando[kvp.Key] = kvp.Value;
                }
            }

            return(expando);
        }