Esempio n. 1
0
        private object ParseDictionary(Dictionary <string, object> d, Dictionary <string, object> globaltypes, Type type, object input)
        {
            object tn = "";

            if (d.TryGetValue("$types", out tn))
            {
                _globalTypes = true;
                if (globaltypes == null)
                {
                    globaltypes = new Dictionary <string, object>();
                }
                foreach (var kv in (Dictionary <string, object>)tn)
                {
                    globaltypes.Add((string)kv.Key, kv.Value);
                }
            }

            bool found = d.TryGetValue("$type", out tn);

#if !SILVERLIGHT
            if (found == false && type == typeof(System.Object))
            {
                return(CreateDataset(d, globaltypes));
            }
#endif
            if (found)
            {
                if (_globalTypes && globaltypes != null)
                {
                    object tname = "";
                    if (globaltypes.TryGetValue((string)tn, out tname))
                    {
                        tn = tname;
                    }
                }
                type = Reflection.Instance.GetTypeFromCache((string)tn);
            }

            if (type == null)
            {
                throw new Exception("Cannot determine type");
            }

            string typename = type.FullName;
            object o        = input;
            if (o == null)
            {
                o = Reflection.Instance.FastCreateInstance(type);
            }
            SafeDictionary <string, myPropInfo> props = Getproperties(type, typename);
            foreach (string name in d.Keys)
            {
                myPropInfo pi;
                if (props.TryGetValue(name, out pi) == false)
                {
                    continue;
                }
                if ((pi.Flags & (myPropInfoFlags.Filled | myPropInfoFlags.CanWrite)) != 0)
                {
                    object v = d[name];

                    if (v != null)
                    {
                        object oset = v;

                        switch (pi.Type)
                        {
#if !SILVERLIGHT
                        case myPropInfoType.DataSet:
                            oset = CreateDataset((Dictionary <string, object>)v, globaltypes);
                            break;

                        case myPropInfoType.DataTable:
                            oset = CreateDataTable((Dictionary <string, object>)v, globaltypes);
                            break;
#endif
                        case myPropInfoType.Custom:
                            oset = CreateCustom((string)v, pi.pt);
                            break;

                        case myPropInfoType.Enum:
                            oset = CreateEnum(pi.pt, (string)v);
                            break;

                        case myPropInfoType.StringDictionary:
                            oset = CreateStringKeyDictionary((Dictionary <string, object>)v, pi.pt, pi.GenericTypes, globaltypes);
                            break;

                        case myPropInfoType.Hashtable:
                        case myPropInfoType.Dictionary:
                            oset = CreateDictionary((List <object>)v, pi.pt, pi.GenericTypes, globaltypes);
                            break;

                        case myPropInfoType.Array:
                            oset = CreateArray((List <object>)v, pi.pt, pi.bt, globaltypes);
                            break;

                        default:
                        {
                            if (pi.IsGenericType && pi.IsValueType == false)
                            {
                                oset = CreateGenericList((List <object>)v, pi.pt, pi.bt, globaltypes);
                            }
                            else if (pi.IsClass && v is Dictionary <string, object> )
                            {
                                oset = ParseDictionary((Dictionary <string, object>)v, globaltypes, pi.pt, input);
                            }

                            else if (v is List <object> )
                            {
                                oset = CreateArray((List <object>)v, pi.pt, typeof(object), globaltypes);
                            }
                            break;
                        }
                        }

                        o = pi.setter(o, oset);
                    }
                }
            }
            return(o);
        }
Esempio n. 2
0
        internal Getters[] GetGetters(Type type, BJSONParameters param)
        {
            Getters[] val = null;
            if (_getterscache.TryGetValue(type, out val))
            {
                return(val);
            }

            PropertyInfo[] props   = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            List <Getters> getters = new List <Getters>();

            foreach (PropertyInfo p in props)
            {
                // CHANGED FOR ** LITEDB ** IgnoreProperty
                if (param.IgnoreProperty != null && p.MetadataToken == param.IgnoreProperty.MetadataToken && p.Module.Equals(param.IgnoreProperty.Module))
                {
                    continue;
                }
                if (!p.CanWrite && param.ShowReadOnlyProperties == false)
                {
                    continue;
                }
                if (param.IgnoreAttributes != null)
                {
                    bool found = false;
                    foreach (var ignoreAttr in param.IgnoreAttributes)
                    {
                        if (p.IsDefined(ignoreAttr, false))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }
                }
                GenericGetter g = CreateGetMethod(type, p);
                if (g != null)
                {
                    getters.Add(new Getters {
                        Getter = g, Name = p.Name
                    });
                }
            }

            FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
            foreach (var f in fi)
            {
                if (param.IgnoreAttributes != null)
                {
                    bool found = false;
                    foreach (var ignoreAttr in param.IgnoreAttributes)
                    {
                        if (f.IsDefined(ignoreAttr, false))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }
                }

                GenericGetter g = CreateGetField(type, f);
                if (g != null)
                {
                    getters.Add(new Getters {
                        Getter = g, Name = f.Name
                    });
                }
            }

            val = getters.ToArray();
            _getterscache.Add(type, val);
            return(val);
        }
Esempio n. 3
0
        internal Getters[] GetGetters(Type type, bool ShowReadOnlyProperties, List <Type> IgnoreAttributes)//JSONParameters param)
        {
            Getters[] val = null;
            if (_getterscache.TryGetValue(type, out val))
            {
                return(val);
            }

            PropertyInfo[] props   = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
            List <Getters> getters = new List <Getters>();

            foreach (PropertyInfo p in props)
            {
                if (p.GetIndexParameters().Length > 0)
                {// Property is an indexer
                    continue;
                }
                if (!p.CanWrite && ShowReadOnlyProperties == false)
                {
                    continue;
                }
                if (IgnoreAttributes != null)
                {
                    bool found = false;
                    foreach (var ignoreAttr in IgnoreAttributes)
                    {
                        if (p.IsDefined(ignoreAttr, false))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }
                }
                GenericGetter g = CreateGetMethod(type, p);
                if (g != null)
                {
                    getters.Add(new Getters {
                        Getter = g, Name = p.Name, lcName = p.Name.ToLower()
                    });
                }
            }

            FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
            foreach (var f in fi)
            {
                if (IgnoreAttributes != null)
                {
                    bool found = false;
                    foreach (var ignoreAttr in IgnoreAttributes)
                    {
                        if (f.IsDefined(ignoreAttr, false))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }
                }
                if (f.IsLiteral == false)
                {
                    GenericGetter g = CreateGetField(type, f);
                    if (g != null)
                    {
                        getters.Add(new Getters {
                            Getter = g, Name = f.Name, lcName = f.Name.ToLower()
                        });
                    }
                }
            }
            val = getters.ToArray();
            _getterscache.Add(type, val);
            return(val);
        }