Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static Dictionary <string, object> GetDictionary(object o, List <string> propertiesToInclude = null)
        {
            var dic = new Dictionary <string, object>();

            // Support Dictionary and ExpandoObect
            if (o is IDictionary <string, object> )
            {
                // We have to make a copy, because returning an ExpandoObject as an IDictionary, do not support the dic["name"] syntax.
                foreach (KeyValuePair <string, object> v in ((IDictionary <string, object>)o))
                {
                    if ((propertiesToInclude == null) || (propertiesToInclude.Contains(v.Key)))
                    {
                        dic.Add(v.Key, v.Value);
                    }
                }
                return(dic);
            }
            #if USE_FAST_REFLECTION
            var typeFullName = o.GetType().FullName;
            if (!__TypeDefined.ContainsKey(typeFullName))
            {
                var l = new List <string>();
                foreach (var k in DynamicSugar.ReflectionHelper.GetDictionaryReflection(o))
                {
                    l.Add(k.Key);
                }
                __TypeDefined.Add(typeFullName, l);
            }
            foreach (var p in __TypeDefined[typeFullName])
            {
                var pp = o.GetType().GetProperty(p);
                if (pp == null)
                {
                    throw new System.ApplicationException("Field '{0}' in object '{1}' is not supported. Only properties are supported".FormatString(p, o));
                }
                dic.Add(p, FastProperty <object, object> .Make(pp).Get(o));
            }
            #else
            dic = DynamicSugar.ReflectionHelper.GetDictionaryReflection(o, propertiesToInclude);
            #endif
            return(dic);
        }