Exemple #1
0
        //private static bool _isWindows = false;

        private static bool TryGetOpCode(short code, out OpCode opCode)
        {
            if (_opCodes != null)
            {
                return(_opCodes.TryGetValue(code, out opCode));
            }
            var dict = new SafeDictionary <short, OpCode>();

            foreach (var fi in typeof(OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                if (!typeof(OpCode).IsAssignableFrom(fi.FieldType))
                {
                    continue;
                }
                var innerOpCode = (OpCode)fi.GetValue(null);
                if (innerOpCode.OpCodeType != OpCodeType.Nternal)
                {
                    dict.Add(innerOpCode.Value, innerOpCode);
                }
            }
            _opCodes = dict;
            return(_opCodes.TryGetValue(code, out opCode));
        }
Exemple #2
0
        internal Getters[] GetGetters(Type type, bool ShowReadOnlyProperties, List <Type> IgnoreAttributes)
        {
            Getters[] val = null;
            if (_getterscache.TryGetValue(type, out val))
            {
                return(val);
            }
            //bool isAnonymous = IsAnonymousType(type);

            var bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;

            //if (ShowReadOnlyProperties)
            //    bf |= BindingFlags.NonPublic;
            PropertyInfo[] props   = type.GetProperties(bf);
            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))//|| isAnonymous == false))
                {
                    continue;
                }
                if (IgnoreAttributes != null)
                {
                    bool found = false;
                    foreach (var ignoreAttr in IgnoreAttributes)
                    {
                        if (p.IsDefined(ignoreAttr, false))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }
                }
                string mName = null;
                #if net4
                var att = p.GetCustomAttributes(true);
                foreach (var at in att)
                {
                    if (at is DataMemberAttribute)
                    {
                        var dm = (DataMemberAttribute)at;
                        if (dm.Name != "")
                        {
                            mName = dm.Name;
                        }
                    }
                }
                #endif
                GenericGetter g = CreateGetMethod(type, p);
                if (g != null)
                {
                    getters.Add(new Getters {
                        Getter = g, Name = p.Name, lcName = p.Name.ToLower(), memberName = mName, member = p
                    });
                }
            }

            FieldInfo[] fi = type.GetFields(bf);
            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;
                    }
                }
                string mName = null;
#if net4
                var att = f.GetCustomAttributes(true);
                foreach (var at in att)
                {
                    if (at is DataMemberAttribute)
                    {
                        var dm = (DataMemberAttribute)at;
                        if (dm.Name != "")
                        {
                            mName = dm.Name;
                        }
                    }
                }
#endif
                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(), memberName = mName, member = f
                        });
                    }
                }
            }
            val = getters.ToArray();
            _getterscache.Add(type, val);
            return(val);
        }
Exemple #3
0
        public Dictionary <string, myPropInfo> Getproperties(Type type, string typename)
        {
            Dictionary <string, myPropInfo> sd = null;

            if (_propertycache.TryGetValue(typename, out sd))
            {
                return(sd);
            }
            else
            {
                sd = new Dictionary <string, myPropInfo>();
                var            bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
                PropertyInfo[] pr = type.GetProperties(bf);
                foreach (PropertyInfo p in pr)
                {
                    if (p.GetIndexParameters().Length > 0)// Property is an indexer
                    {
                        continue;
                    }

                    myPropInfo d = CreateMyProp(p.PropertyType, p.Name);
                    d.setter = Reflection.CreateSetMethod(type, p);
                    if (d.setter != null)
                    {
                        d.CanWrite = true;
                    }
                    d.getter = Reflection.CreateGetMethod(type, p);
#if net4
                    var att = p.GetCustomAttributes(true);
                    foreach (var at in att)
                    {
                        if (at is DataMemberAttribute)
                        {
                            var dm = (DataMemberAttribute)at;
                            if (dm.Name != "")
                            {
                                d.memberName = dm.Name;
                            }
                        }
                    }
                    if (d.memberName != null)
                    {
                        sd.Add(d.memberName, d);
                    }
                    else
#endif
                    sd.Add(p.Name.ToLower(), d);
                }
                FieldInfo[] fi = type.GetFields(bf);
                foreach (FieldInfo f in fi)
                {
                    myPropInfo d = CreateMyProp(f.FieldType, f.Name);
                    if (f.IsLiteral == false)
                    {
                        d.setter = Reflection.CreateSetField(type, f);
                        if (d.setter != null)
                        {
                            d.CanWrite = true;
                        }
                        d.getter = Reflection.CreateGetField(type, f);
#if net4
                        var att = f.GetCustomAttributes(true);
                        foreach (var at in att)
                        {
                            if (at is DataMemberAttribute)
                            {
                                var dm = (DataMemberAttribute)at;
                                if (dm.Name != "")
                                {
                                    d.memberName = dm.Name;
                                }
                            }
                        }
                        if (d.memberName != null)
                        {
                            sd.Add(d.memberName, d);
                        }
                        else
#endif
                        sd.Add(f.Name.ToLower(), d);
                    }
                }

                _propertycache.Add(typename, sd);
                return(sd);
            }
        }
Exemple #4
0
        private object ParseDictionary(Dictionary <string, object> d, Dictionary <string, object> globaltypes, Type type, object input)
        {
            object tn = "";

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

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

#if !SILVERLIGHT && ADONET
            if (found == false && type == typeof(System.Object))
            {
                return(d);   // CreateDataset(d, globaltypes);
            }
#endif
            if (found)
            {
                if (_usingglobals)
                {
                    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 n in d.Keys)
            {
                string name = n;
                if (_params.IgnoreCaseOnDeserialize)
                {
                    name = name.ToLower();
                }
                if (name == "$map")
                {
                    ProcessMap(o, props, (Dictionary <string, object>)d[name]);
                    continue;
                }
                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 = null;

                        switch (pi.Type)
                        {
                        case myPropInfoType.Short: oset = Convert.ToInt16(v); break;

                        case myPropInfoType.Int: oset = Convert.ToInt32(v); break;

                        case myPropInfoType.Float: oset = Convert.ToSingle(v); break;

                        case myPropInfoType.Long: oset = (long)v; break;

                        case myPropInfoType.String: oset = (string)v; break;

                        case myPropInfoType.Bool: oset = (bool)v; break;

                        case myPropInfoType.DateTime: oset = CreateDateTime((string)v); break;

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

                        case myPropInfoType.Guid: oset = CreateGuid((string)v); break;

                        case myPropInfoType.Array:
                            if (!pi.IsValueType)
                            {
                                oset = CreateArray((List <object>)v, pi.pt, pi.bt, globaltypes);
                            }
                            // what about 'else'?
                            break;

                        case myPropInfoType.ByteArray: oset = Convert.FromBase64String((string)v); break;

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

                        case myPropInfoType.DataTable: oset = this.CreateDataTable((Dictionary <string, object>)v, globaltypes); break;

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

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

                        case myPropInfoType.Custom: oset = CreateCustom((string)v, pi.pt); break;

                        default:
                        {
                            if (pi.IsGenericType && pi.IsValueType == false && v is List <object> )
                            {
                                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, pi.getter(o));
                            }

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

                            else if (pi.IsValueType)
                            {
                                oset = ChangeType(v, pi.changeType);
                            }

                            else
                            {
                                oset = v;
                            }
                        }
                        break;
                        }

                        o = pi.setter(o, oset);
                    }
                }
            }
            return(o);
        }
Exemple #5
0
        private object ParseDictionary(Dictionary <string, object> d, Dictionary <string, object> globaltypes, Type type, object input)
        {
            object tn = "";

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

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

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

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

            string typename = type.FullName;
            object o        = input;
            if (o == null)
            {
                o = FastCreateInstance(type);
            }
            SafeDictionary <string, myPropInfo> props = Getproperties(type, typename);
            foreach (string n in d.Keys)
            {
                string name = n;
                if (_params.IgnoreCaseOnDeserialize)
                {
                    name = name.ToLower();
                }
                if (name == "$map")
                {
                    ProcessMap(o, props, (Dictionary <string, object>)d[name]);
                    continue;
                }
                myPropInfo pi;
                if (props.TryGetValue(name, out pi) == false)
                {
                    continue;
                }
                if (pi.filled == true)
                {
                    object v = d[name];

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

                        if (pi.isInt)
                        {
                            oset = (int)CreateLong((string)v);
                        }
#if CUSTOMTYPE
                        else if (pi.isCustomType)
                        {
                            oset = CreateCustom((string)v, pi.pt);
                        }
#endif
                        else if (pi.isLong)
                        {
                            oset = CreateLong((string)v);
                        }

                        else if (pi.isString)
                        {
                            oset = (string)v;
                        }

                        else if (pi.isBool)
                        {
                            oset = (bool)v;
                        }

                        else if (pi.isGenericType && pi.isValueType == false && pi.isDictionary == false)
#if SILVERLIGHT
                        { oset = CreateGenericList((List <object>)v, pi.pt, pi.bt, globaltypes); }
#else
                        { oset = CreateGenericList((ArrayList)v, pi.pt, pi.bt, globaltypes); }
#endif
                        else if (pi.isByteArray)
                        {
                            oset = Convert.FromBase64String((string)v);
                        }

                        else if (pi.isArray && pi.isValueType == false)
#if SILVERLIGHT
                        { oset = CreateArray((List <object>)v, pi.pt, pi.bt, globaltypes); }
#else
                        { oset = CreateArray((ArrayList)v, pi.pt, pi.bt, globaltypes); }
#endif
                        else if (pi.isGuid)
                        {
                            oset = CreateGuid((string)v);
                        }
#if !SILVERLIGHT
                        else if (pi.isDataSet)
                        {
                            oset = CreateDataset((Dictionary <string, object>)v, globaltypes);
                        }

                        else if (pi.isDataTable)
                        {
                            oset = this.CreateDataTable((Dictionary <string, object>)v, globaltypes);
                        }
#endif

                        else if (pi.isStringDictionary)
                        {
                            oset = CreateStringKeyDictionary((Dictionary <string, object>)v, pi.pt, pi.GenericTypes, globaltypes);
                        }

#if !SILVERLIGHT
                        else if (pi.isDictionary || pi.isHashtable)
                        {
                            oset = CreateDictionary((ArrayList)v, pi.pt, pi.GenericTypes, globaltypes);
                        }
#else
                        else if (pi.isDictionary)
                        {
                            oset = CreateDictionary((List <object>)v, pi.pt, pi.GenericTypes, globaltypes);
                        }
#endif

                        else if (pi.isEnum)
                        {
                            oset = CreateEnum(pi.pt, (string)v);
                        }

                        else if (pi.isDateTime)
                        {
                            oset = CreateDateTime((string)v);
                        }

                        else if (pi.isClass && v is Dictionary <string, object> )
                        {
                            oset = ParseDictionary((Dictionary <string, object>)v, globaltypes, pi.pt, null);
                        }

                        else if (pi.isValueType)
                        {
                            oset = ChangeType(v, pi.changeType);
                        }

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

                        if (pi.CanWrite)
                        {
                            pi.setter(o, oset);
                        }
                    }
                }
Exemple #6
0
        internal bool IsTypeRegistered(Type t)
        {
            Serialize s;

            return(_customSerializer.TryGetValue(t, out s));
        }
        public 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);
        }
Exemple #8
0
        internal Getters[] GetGetters(Type type, bool showReadOnlyProperties, List <Type> ignoreAttributes)
        {
            Getters[] val;
            if (_getterscache.TryGetValue(type, out val))
            {
                return(val);
            }

            //bool isAnonymous = IsAnonymousType(type);

            //if (ShowReadOnlyProperties)
            //    bf |= BindingFlags.NonPublic;
            var props   = type.GetProperties(Bf);
            var getters = new List <Getters>();

            foreach (var p in props)
            {
                if (p.GetIndexParameters().Length > 0)
                {// Property is an indexer
                    continue;
                }
                if (!p.CanWrite && (showReadOnlyProperties == false))//|| isAnonymous == false))
                {
                    continue;
                }
                if (ignoreAttributes != null)
                {
                    var found = false;
                    foreach (var ignoreAttr in ignoreAttributes)
                    {
                        if (!p.IsDefined(ignoreAttr, false))
                        {
                            continue;
                        }

                        found = true;
                        break;
                    }
                    if (found)
                    {
                        continue;
                    }
                }
                var g = CreateGetMethod(type, p);
                if (g != null)
                {
                    getters.Add(new Getters {
                        Getter = g, Name = p.Name, LcName = p.Name.ToLower()
                    });
                }
            }

            var fi = type.GetFields(Bf);

            foreach (var f in fi)
            {
                if (ignoreAttributes != null)
                {
                    var found = false;
                    foreach (var ignoreAttr in ignoreAttributes)
                    {
                        if (!f.IsDefined(ignoreAttr, false))
                        {
                            continue;
                        }

                        found = true;
                        break;
                    }
                    if (found)
                    {
                        continue;
                    }
                }

                if (f.IsLiteral)
                {
                    continue;
                }

                var 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);
        }
Exemple #9
0
        public Getters[] GetGetters(Type type, /*bool ShowReadOnlyProperties,*/ List <Type> IgnoreAttributes)
        {
            Getters[] val = null;
            if (_getterscache.TryGetValue(type, out val))
            {
                return(val);
            }

            var bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;

            //if (ShowReadOnlyProperties)
            //    bf |= BindingFlags.NonPublic;
            PropertyInfo[] props   = type.GetProperties(bf);
            List <Getters> getters = new List <Getters>();

            foreach (PropertyInfo p in props)
            {
                bool read_only = false;
                if (p.GetIndexParameters().Length > 0)
                {// Property is an indexer
                    continue;
                }
                if (!p.CanWrite)      // && (ShowReadOnlyProperties == false))//|| isAnonymous == false))
                {
                    read_only = true; //continue;
                }
                if (IgnoreAttributes != null)
                {
                    bool found = false;
                    foreach (var ignoreAttr in IgnoreAttributes)
                    {
                        if (p.IsDefined(ignoreAttr, false))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }
                }
                string mName = null;
                var    att   = p.GetCustomAttributes(true);
                foreach (var at in att)
                {
#if NET4
                    if (at is System.Runtime.Serialization.DataMemberAttribute)
                    {
                        var dm = (System.Runtime.Serialization.DataMemberAttribute)at;
                        if (dm.Name != "")
                        {
                            mName = dm.Name;
                        }
                    }
#endif
                    if (at is fastJSON.DataMemberAttribute)
                    {
                        var dm = (fastJSON.DataMemberAttribute)at;
                        if (dm.Name != "")
                        {
                            mName = dm.Name;
                        }
                    }
                }
                GenericGetter g = (object obj) => { return(p.GetValue(obj)); };
                if (g != null)
                {
                    getters.Add(new Getters {
                        Getter = g, Name = p.Name, lcName = p.Name.ToLowerInvariant(), memberName = mName, ReadOnly = read_only
                    });
                }
            }

            FieldInfo[] fi = type.GetFields(bf);
            foreach (var f in fi)
            {
                bool read_only = false;
                if (f.IsInitOnly)     // && (ShowReadOnlyProperties == false))//|| isAnonymous == false))
                {
                    read_only = true; //continue;
                }
                if (IgnoreAttributes != null)
                {
                    bool found = false;
                    foreach (var ignoreAttr in IgnoreAttributes)
                    {
                        if (f.IsDefined(ignoreAttr, false))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }
                }
                string mName = null;
                var    att   = f.GetCustomAttributes(true);
                foreach (var at in att)
                {
#if NET4
                    if (at is System.Runtime.Serialization.DataMemberAttribute)
                    {
                        var dm = (System.Runtime.Serialization.DataMemberAttribute)at;
                        if (dm.Name != "")
                        {
                            mName = dm.Name;
                        }
                    }
#endif
                    if (at is fastJSON.DataMemberAttribute)
                    {
                        var dm = (fastJSON.DataMemberAttribute)at;
                        if (dm.Name != "")
                        {
                            mName = dm.Name;
                        }
                    }
                }
                if (f.IsLiteral == false)
                {
                    GenericGetter g = CreateGetField(type, f);
                    if (g != null)
                    {
                        getters.Add(new Getters {
                            Getter = g, Name = f.Name, lcName = f.Name.ToLowerInvariant(), memberName = mName, ReadOnly = read_only
                        });
                    }
                }
            }
            val = getters.ToArray();
            _getterscache.Add(type, val);
            return(val);
        }
Exemple #10
0
        public Dictionary <string, myPropInfo> Getproperties(Type type, string typename, bool ShowReadOnlyProperties)
        {
            Dictionary <string, myPropInfo> sd = null;

            if (_propertycache.TryGetValue(typename, out sd))
            {
                return(sd);
            }
            else
            {
                sd = new Dictionary <string, myPropInfo>(10);
                var            bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
                PropertyInfo[] pr = type.GetProperties(bf);
                foreach (PropertyInfo p in pr)
                {
                    if (p.GetIndexParameters().Length > 0)// Property is an indexer
                    {
                        continue;
                    }

                    myPropInfo d = CreateMyProp(p.PropertyType, p.Name);

                    d.CanWrite = p.CanWrite;
                    if (p.CanWrite)
                    {
                        d.setter = (object target, object value) => { p.SetValue(target, value); return(target); }
                    }
                    ;

                    d.getter = (object obj) => { return(p.GetValue(obj)); };

                    var att = p.GetCustomAttributes(true);
                    foreach (var at in att)
                    {
#if NET4
                        if (at is System.Runtime.Serialization.DataMemberAttribute)
                        {
                            var dm = (System.Runtime.Serialization.DataMemberAttribute)at;
                            if (dm.Name != "")
                            {
                                d.memberName = dm.Name;
                            }
                        }
#endif
                        if (at is fastJSON.DataMemberAttribute)
                        {
                            var dm = (fastJSON.DataMemberAttribute)at;
                            if (dm.Name != "")
                            {
                                d.memberName = dm.Name;
                            }
                        }
                    }
                    if (d.memberName != null)
                    {
                        sd.Add(d.memberName, d);
                    }
                    else
                    {
                        sd.Add(p.Name.ToLowerInvariant(), d);
                    }
                }
                FieldInfo[] fi = type.GetFields(bf);
                foreach (FieldInfo f in fi)
                {
                    myPropInfo d = CreateMyProp(f.FieldType, f.Name);
                    if (f.IsLiteral == false)
                    {
                        if (f.IsInitOnly == false)
                        {
                            d.setter = (object target, object value) => { f.SetValue(target, value); return(target); }
                        }
                        ;                                                                                              //Reflection.CreateSetField(type, f);
                        if (d.setter != null)
                        {
                            d.CanWrite = true;
                        }

                        d.getter = (object obj) => { return(f.GetValue(obj)); };

                        var att = f.GetCustomAttributes(true);
                        foreach (var at in att)
                        {
#if NET4
                            if (at is System.Runtime.Serialization.DataMemberAttribute)
                            {
                                var dm = (System.Runtime.Serialization.DataMemberAttribute)at;
                                if (dm.Name != "")
                                {
                                    d.memberName = dm.Name;
                                }
                            }
#endif
                            if (at is fastJSON.DataMemberAttribute)
                            {
                                var dm = (fastJSON.DataMemberAttribute)at;
                                if (dm.Name != "")
                                {
                                    d.memberName = dm.Name;
                                }
                            }
                        }
                        if (d.memberName != null)
                        {
                            sd.Add(d.memberName, d);
                        }
                        else
                        {
                            sd.Add(f.Name.ToLowerInvariant(), d);
                        }
                    }
                }

                _propertycache.Add(typename, sd);
                return(sd);
            }
        }
Exemple #11
0
        internal Getters[] GetGetters(Type type, JSONParameters 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)
            {
                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);
        }
Exemple #12
0
        public Getters[] GetGetters(Type type, bool ShowReadOnlyProperties, List <Type> IgnoreAttributes)
        {
            Getters[] val = null;
            if (_getterscache.TryGetValue(type, out val))
            {
                return(val);
            }

            var bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
            //if (ShowReadOnlyProperties)
            //    bf |= BindingFlags.NonPublic;
            var props   = type.GetProperties(bf);
            var getters = new List <Getters>();

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

            var fi = type.GetFields(bf);

            foreach (var f in fi)
            {
                if (IgnoreAttributes != null)
                {
                    var found = false;
                    foreach (var ignoreAttr in IgnoreAttributes)
                    {
                        if (f.IsDefined(ignoreAttr, false))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }
                }
                string mName = null;
                if (f.IsLiteral == false)
                {
                    var g = CreateGetField(type, f);
                    if (g != null)
                    {
                        getters.Add(new Getters {
                            Getter = g, Name = f.Name, lcName = f.Name.ToLowerInvariant(), memberName = mName
                        });
                    }
                }
            }
            val = getters.ToArray();
            _getterscache.Add(type, val);
            return(val);
        }
Exemple #13
0
        private object ParseDictionary(Dictionary <string, object> d, Type type)
        {
            object tn    = "";
            bool   found = d.TryGetValue("$type", out tn);

            if (found == false && type == typeof(System.Object))
            {
                return(CreateDataset(d));
            }

            if (found)
            {
                type = GetTypeFromCache((string)tn);
            }

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

            string typename = type.Name;
            object o        = FastCreateInstance(type);
            SafeDictionary <string, myPropInfo> props = Getproperties(type, typename);

            foreach (string name in d.Keys)
            {
                if (name == "$map")
                {
                    ProcessMap(o, props, (Dictionary <string, object>)d[name]);
                    continue;
                }
                myPropInfo pi;
                if (props.TryGetValue(name, out pi) == false)
                {
                    continue;
                }
                if (pi.filled == true)
                {
                    object v = d[name];

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

                        if (pi.isInt)
                        {
                            oset = (int)CreateLong((string)v);
                        }

                        else if (pi.isLong)
                        {
                            oset = CreateLong((string)v);
                        }

                        else if (pi.isString)
                        {
                            oset = (string)v;
                        }

                        else if (pi.isBool)
                        {
                            oset = (bool)v;
                        }

                        else if (pi.isGenericType && pi.isValueType == false && pi.isDictionary == false)
                        {
                            oset = CreateGenericList((ArrayList)v, pi.pt, pi.bt);
                        }

                        else if (pi.isByteArray)
                        {
                            oset = Convert.FromBase64String((string)v);
                        }

                        else if (pi.isArray && pi.isValueType == false)
                        {
                            oset = CreateArray((ArrayList)v, pi.pt, pi.bt);
                        }

                        else if (pi.isGuid)
                        {
                            oset = CreateGuid((string)v);
                        }

                        else if (pi.isDataSet)
                        {
                            oset = CreateDataset((Dictionary <string, object>)v);
                        }

                        else if (pi.isStringDictionary)
                        {
                            oset = CreateStringKeyDictionary((Dictionary <string, object>)v, pi.pt, pi.GenericTypes);
                        }

                        else if (pi.isDictionary || pi.isHashtable)
                        {
                            oset = CreateDictionary((ArrayList)v, pi.pt, pi.GenericTypes);
                        }

                        else if (pi.isEnum)
                        {
                            oset = CreateEnum(pi.pt, (string)v);
                        }

                        else if (pi.isDateTime)
                        {
                            oset = CreateDateTime((string)v);
                        }

                        else if (pi.isClass && v is Dictionary <string, object> )
                        {
                            oset = ParseDictionary((Dictionary <string, object>)v, pi.pt);
                        }

                        else if (pi.isValueType)
                        {
                            oset = ChangeType(v, pi.changeType);
                        }

                        else if (v is ArrayList)
                        {
                            oset = CreateArray((ArrayList)v, pi.pt, typeof(object));
                        }

                        else
                        {
                            oset = v;
                        }

                        pi.setter(o, oset);
                    }
                }
            }
            return(o);
        }
Exemple #14
0
        public Dictionary <string, myPropInfo> Getproperties(Type type, string typename, bool customType)
        {
            Dictionary <string, myPropInfo> sd = null;

            if (_propertycache.TryGetValue(typename, out sd))
            {
                return(sd);
            }
            else
            {
                sd = new Dictionary <string, myPropInfo>();
                PropertyInfo[] pr = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                foreach (PropertyInfo p in pr)
                {
                    bool pass = false;
                    foreach (var attrtype in JSON.Parameters.IgnoreAttributes)
                    {
                        if (p.IsDefined(attrtype, true))
                        {
                            pass = true;
                            break;
                        }
                    }

                    if (pass)
                    {
                        continue;
                    }

                    if (p.GetIndexParameters().Length > 0)
                    {// Property is an indexer
                        continue;
                    }
                    myPropInfo d = CreateMyProp(p.PropertyType, p.Name, customType);

                    d.setter = Reflection.CreateSetMethod(type, p);
                    if (d.setter != null && p.CanWrite)
                    {
                        d.CanWrite = true;
                    }
                    d.getter = Reflection.CreateGetMethod(type, p);
                    sd.Add(p.Name.ToLower(), d);
                }
                FieldInfo[] fi = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                foreach (FieldInfo f in fi)
                {
                    myPropInfo d = CreateMyProp(f.FieldType, f.Name, customType);
                    if (f.IsLiteral == false)
                    {
                        d.setter = Reflection.CreateSetField(type, f);
                        if (d.setter != null)
                        {
                            d.CanWrite = true;
                        }
                        d.getter = Reflection.CreateGetField(type, f);
                        sd.Add(f.Name.ToLower(), d);
                    }
                }

                _propertycache.Add(typename, sd);
                return(sd);
            }
        }