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

            Initialize(expando);

            Properties = new ConcurrentDictionary <string, object>();

            foreach (var(key, kvpValue) in dict)
            {
                switch (kvpValue)
                {
                case IDictionary <string, object> _:
                    var expandoVal = new Expando(kvpValue);
                    expando[key] = expandoVal;
                    break;

                case ICollection collection:
                    var objList = new List <object>();
                    foreach (var item in collection)
                    {
                        if (item is IDictionary <string, object> )
                        {
                            var expandoItem = new Expando(item);
                            objList.Add(expandoItem);
                        }
                        else
                        {
                            objList.Add(item);
                        }
                    }
                    expando[key] = objList;
                    break;

                default:
                    expando[key] = kvpValue;
                    break;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Convert expando class to a typed object.
        /// </summary>
        /// <typeparam name="TObject">typed object</typeparam>
        /// <param name="expando">expando class</param>
        /// <returns></returns>
        public static TObject ToObject <TObject>(this Expando expando) where TObject : class
        {
            var json = JObject.FromObject(expando);

            return(json.ToObject <TObject>());
        }