Exemplo n.º 1
0
 internal void ClearReflectionCache()
 {
     _tyname         = new SafeDictionary <Type, string>();
     _typecache      = new SafeDictionary <string, Type>();
     _constrcache    = new SafeDictionary <Type, CreateObject>();
     _getterscache   = new SafeDictionary <Type, Getters[]>();
     _propertycache  = new SafeDictionary <string, Dictionary <string, myPropInfo> >();
     _genericTypes   = new SafeDictionary <Type, Type[]>();
     _genericTypeDef = new SafeDictionary <Type, Type>();
 }
Exemplo n.º 2
0
 public void RegisterCustomType(Type type, Serialize serializer, Deserialize deserializer)
 {
     if (type != null && serializer != null && deserializer != null)
     {
         _customSerializer.Add(type, serializer);
         _customDeserializer.Add(type, deserializer);
         // reset property cache
         _propertycache = new SafeDictionary <string, SafeDictionary <string, myPropInfo> >();
     }
 }
Exemplo n.º 3
0
        private SafeDictionary <string, myPropInfo> getproperty(Type type, string typename)
        {
            SafeDictionary <string, myPropInfo> sd = null;

            if (_propertycache.TryGetValue(typename, out sd))
            {
                return(sd);
            }
            else
            {
                sd = new SafeDictionary <string, myPropInfo>();
                PropertyInfo[] pr = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                foreach (PropertyInfo p in pr)
                {
                    myPropInfo d = new myPropInfo();
                    d.filled       = true;
                    d.pt           = p.PropertyType;
                    d.isDictionary = p.PropertyType.Name.Contains("Dictionary");
                    if (d.isDictionary)
                    {
                        d.GenericTypes = p.PropertyType.GetGenericArguments();
                    }
                    d.isValueType   = p.PropertyType.IsValueType;
                    d.isGenericType = p.PropertyType.IsGenericType;
                    d.isArray       = p.PropertyType.IsArray;
                    if (d.isArray)
                    {
                        d.bt = p.PropertyType.GetElementType();
                    }
                    if (d.isGenericType)
                    {
                        d.bt = p.PropertyType.GetGenericArguments()[0];
                    }
                    d.isByteArray = p.PropertyType == typeof(byte[]);
                    d.isGuid      = (p.PropertyType == typeof(Guid) || p.PropertyType == typeof(Guid?));
                    d.isHashtable = p.PropertyType == typeof(Hashtable);
                    d.isDataSet   = p.PropertyType == typeof(DataSet);
                    d.setter      = CreateSetMethod(p);
                    d.changeType  = GetChangeType(p.PropertyType);
                    d.isEnum      = p.PropertyType.IsEnum;
                    d.isDateTime  = p.PropertyType == typeof(DateTime) || p.PropertyType == typeof(DateTime?);
                    d.isInt       = p.PropertyType == typeof(int) || p.PropertyType == typeof(int?);
                    d.isLong      = p.PropertyType == typeof(long) || p.PropertyType == typeof(long?);
                    d.isString    = p.PropertyType == typeof(string);
                    d.isBool      = p.PropertyType == typeof(bool) || p.PropertyType == typeof(bool?);
                    d.isClass     = p.PropertyType.IsClass;
                    d.getter      = CreateGetMethod(p);

                    sd.Add(p.Name, d);
                }
                _propertycache.Add(typename, sd);
                return(sd);
            }
        }
Exemplo n.º 4
0
 private void SetupLookup()
 {
     _lookup = new SafeDictionary <string, bool>();
     _seen   = new SafeDictionary <Type, bool>();
     _lookup.Add("$types", true);
     _lookup.Add("$type", true);
     _lookup.Add("$i", true);
     _lookup.Add("$map", true);
     _lookup.Add("$schema", true);
     _lookup.Add("k", true);
     _lookup.Add("v", true);
 }
Exemplo n.º 5
0
        private SafeDictionary <string, myPropInfo> Getproperties(Type type, string typename)
        {
            SafeDictionary <string, myPropInfo> sd = null;

            if (_propertycache.TryGetValue(typename, out sd))
            {
                return(sd);
            }
            else
            {
                sd = new SafeDictionary <string, myPropInfo>();
                PropertyInfo[] pr = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                foreach (PropertyInfo p in pr)
                {
                    // ** DataMemberAttribute ** Added by DuelMonster ** \\
                    string sPropName = p.Name;

                    var dataMemberAttribute = (DataMemberAttribute)p.GetCustomAttributes(typeof(DataMemberAttribute), false).SingleOrDefault();
                    if (dataMemberAttribute != null && !String.IsNullOrEmpty(dataMemberAttribute.Name))
                    {
                        sPropName = dataMemberAttribute.Name;
                    }

                    myPropInfo d = CreateMyProp(p.PropertyType, sPropName);
                    d.Flags |= myPropInfoFlags.CanWrite;
                    d.setter = Reflection.CreateSetMethod(type, p);
                    d.getter = Reflection.CreateGetMethod(type, p);
                    sd.Add(sPropName, d);
                }

                FieldInfo[] fi = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                foreach (FieldInfo f in fi)
                {
                    // ** DataMemberAttribute ** Added by DuelMonster ** \\
                    string sFieldName = f.Name;

                    var dataMemberAttribute = (DataMemberAttribute)f.GetCustomAttributes(typeof(DataMemberAttribute), false).SingleOrDefault();
                    if (dataMemberAttribute != null && !String.IsNullOrEmpty(dataMemberAttribute.Name))
                    {
                        sFieldName = dataMemberAttribute.Name;
                    }

                    myPropInfo d = CreateMyProp(f.FieldType, sFieldName);
                    d.setter = Reflection.CreateSetField(type, f);
                    d.getter = Reflection.CreateGetField(type, f);
                    sd.Add(sFieldName, d);
                }

                _propertycache.Add(typename, sd);
                return(sd);
            }
        }
Exemplo n.º 6
0
 private void ProcessMap(object obj, SafeDictionary <string, JSON.myPropInfo> props, Dictionary <string, object> dic)
 {
     foreach (KeyValuePair <string, object> kv in dic)
     {
         myPropInfo p = props[kv.Key];
         object     o = p.getter(obj);
         Type       t = Type.GetType((string)kv.Value);
         if (t == typeof(Guid))
         {
             p.setter(obj, CreateGuid((string)o));
         }
     }
 }
Exemplo n.º 7
0
        private SafeDictionary <string, myPropInfo> Getproperties(Type type, string typename)
        {
            SafeDictionary <string, myPropInfo> sd = null;

            if (_propertycache.TryGetValue(typename, out sd))
            {
                return(sd);
            }
            else
            {
                sd = new SafeDictionary <string, myPropInfo>();
                PropertyInfo[] pr = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                foreach (PropertyInfo p in pr)
                {
                    myPropInfo d = CreateMyProp(p.PropertyType, p.Name);
                    d.Flags |= myPropInfoFlags.CanWrite;
                    d.setter = Reflection.CreateSetMethod(type, p);
                    d.getter = Reflection.CreateGetMethod(type, p);
                    if (_params.IgnoreCaseOnDeserialize)
                    {
                        sd.Add(p.Name.ToLower(), d);
                    }
                    else
                    {
                        sd.Add(p.Name, d);
                    }
                }
                FieldInfo[] fi = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                foreach (FieldInfo f in fi)
                {
                    myPropInfo d = CreateMyProp(f.FieldType, f.Name);
                    d.setter = Reflection.CreateSetField(type, f);
                    d.getter = Reflection.CreateGetField(type, f);
                    if (_params.IgnoreCaseOnDeserialize)
                    {
                        sd.Add(f.Name.ToLower(), d);
                    }
                    else
                    {
                        sd.Add(f.Name, d);
                    }
                }

                _propertycache.Add(typename, sd);
                return(sd);
            }
        }
Exemplo n.º 8
0
        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));
        }
Exemplo n.º 9
0
        public unsafe object Decode(Type objtype)
        {
            fixed(char *p = json)
            {
                if (objtype != null)
                {
                    if (CheckForTypeInJson(p) == false)
                    {
                        _parseJsonType = true;
                        SetupLookup();

                        BuildLookup(objtype);

                        // reset if no properties found
                        if (_parseJsonType == false || _lookup.Count() == 7)
                        {
                            _lookup = null;
                        }
                    }
                }
                return(ParseValue(p));
            }
        }
Exemplo n.º 10
0
        //private static bool IsAnonymousType(Type type)
        //{
        //    // may break in the future if compiler defined names change...
        //    const string CS_ANONYMOUS_PREFIX = "<>f__AnonymousType";
        //    const string VB_ANONYMOUS_PREFIX = "VB$AnonymousType";

        //    if (type == null)
        //        throw new ArgumentNullException("type");

        //    if (type.Name.StartsWith(CS_ANONYMOUS_PREFIX, StringComparison.Ordinal) || type.Name.StartsWith(VB_ANONYMOUS_PREFIX, StringComparison.Ordinal))
        //    {
        //        return type.IsDefined(typeof(CompilerGeneratedAttribute), false);
        //    }

        //    return false;
        //}
        #endregion

        internal void ResetPropertyCache()
        {
            _propertycache = new SafeDictionary <string, Dictionary <string, myPropInfo> >();
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
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);
                        }
                    }
                }
Exemplo n.º 13
0
        //private static bool IsAnonymousType(Type type)
        //{
        //    // may break in the future if compiler defined names change...
        //    const string CS_ANONYMOUS_PREFIX = "<>f__AnonymousType";
        //    const string VB_ANONYMOUS_PREFIX = "VB$AnonymousType";

        //    if (type == null)
        //        throw new ArgumentNullException("type");

        //    if (type.Name.StartsWith(CS_ANONYMOUS_PREFIX, StringComparison.Ordinal) || type.Name.StartsWith(VB_ANONYMOUS_PREFIX, StringComparison.Ordinal))
        //    {
        //        return type.IsDefined(typeof(CompilerGeneratedAttribute), false);
        //    }

        //    return false;
        //}
        #endregion

        private void ResetPropertyCache()
        {
            _propertycache = new SafeDictionary <string, Dictionary <string, MyPropInfo> >();
        }
Exemplo n.º 14
0
        private object ParseDictionary(Dictionary <string, object> d)
        {
            object tn = "";

            if (d.TryGetValue("$type", out tn) == false)
            {
                return(CreateDataset(d));
            }
            Type   type     = GetTypeFromCache((string)tn);
            string typename = type.Name;
            object o        = FastCreateInstance(type);
            SafeDictionary <string, myPropInfo> props = getproperty(type, typename);

            foreach (string name in d.Keys)
            {
                if (name == "$type")
                {
                    continue;
                }
                if (name == "$map")
                {
                    ProcessMap(o, props, (Dictionary <string, object>)d[name]);
                    continue;
                }
                myPropInfo pi = props[name];
                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.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);
                        }

                        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);
        }