Exemple #1
0
        /// <summary>
        /// 反序列化出一个对象
        /// </summary>
        public object ToDeserialize(Type type)
        {
            switch (ValueType)
            {
            case ValueType.ARRAY:
            {
                return((mValue as JArray).ToDeserialize(type));
            }

            case ValueType.OBJECT:
            {
                return((mValue as JObject).ToDeserialize(type));
            }

            case ValueType.BOOLEAN:
            {
                return("true" == (mValue as string) ? true : false);
            }

            case ValueType.NUMBER:
            {
                return(JUtil.ToObject(type, mValue as string));
            }

            case ValueType.NULL:
            {
                return(null);
            }

            case ValueType.STRING:
            {
                return(JUtil.ToObject(type, mValue as string));
            }

            default:
            {
                return(JUtil.CreateInstance(type));
            }
            }
        }
Exemple #2
0
        /// <summary>
        /// 序列化对象
        /// </summary>
        public object ToDeserialize(Type type)
        {
            if (!JUtil.CanInstance(type))
            {
                return(null);
            }

            if (type.IsArray)
            {
                object defaultValue = JUtil.CreateInstance(type, mValues.Count);
                Type   eleType      = type.GetElementType();
                for (int i = 0; i < mValues.Count; ++i)
                {
                    type.GetMethod("SetValue", new Type[] { typeof(object), typeof(int) }).Invoke(defaultValue, new object[] { mValues[i].ToDeserialize(eleType), i });
                }
                return(defaultValue);
            }
            else
            {
                object defaultValue = JUtil.CreateInstance(type);

                if (JUtil.IsListGenericType(type))
                {
                    if (JUtil.IsListGenericType(type))
                    {
                        Type t = type.GetGenericArguments()[0];
                        for (int i = 0; i < mValues.Count; ++i)
                        {
                            type.GetMethod("Insert", new Type[] { typeof(int), t }).Invoke(defaultValue, new object[] { i, mValues[i].ToDeserialize(t) });
                        }
                        return(defaultValue);
                    }
                }

                if (JUtil.IsDictionaryGenericType(type))
                {
                    Type keyType   = type.GetGenericArguments()[0];
                    Type valueType = type.GetGenericArguments()[1];

                    Type keyValueType = typeof(KeyValuePair <,>).MakeGenericType(new Type[] { keyType, valueType });

                    for (int i = 0; i < mValues.Count; ++i)
                    {
                        object keyVlaue = mValues[i].ToDeserialize(keyValueType);

                        object key = null, value = null;
                        if (JUtil.GetProperty(keyValueType, "Key", keyVlaue, ref key))
                        {
                            JUtil.GetProperty(keyValueType, "Value", keyVlaue, ref value);
                        }

                        if (null != key)
                        {
                            type.GetMethod("Add", new Type[] { keyType, valueType }).Invoke(defaultValue, new object[] { key, value });
                        }
                    }
                }

                return(defaultValue);
            }
        }
Exemple #3
0
        /// <summary>
        /// 序列化对象
        /// </summary>
        public object ToDeserialize(Type type)
        {
            if (!JUtil.CanInstance(type))
            {
                return(null);
            }
            object defalutValue = JUtil.CreateInstance(type);

            if (JUtil.IsValueKeyDictionaryGenericType(type))
            {
                Type keyType   = type.GetGenericArguments()[0];
                Type valueType = type.GetGenericArguments()[1];

                foreach (KeyValuePair <string, JValue> tempKV in mSortPropertys)
                {
                    object key = JUtil.ToObject(keyType, tempKV.Key);
                    if (!(bool)type.GetMethod("ContainsKey", new Type[] { keyType }).Invoke(defalutValue, new object[] { key }))
                    {
                        type.GetMethod("Add", new Type[] { keyType, valueType }).Invoke(defalutValue, new object[] { key, tempKV.Value.ToDeserialize(valueType) });
                    }
                }
                return(defalutValue);
            }

            if (JUtil.IsNameValueCollectionType(type))
            {
                NameValueCollection nameValue = defalutValue as NameValueCollection;

                foreach (KeyValuePair <string, JValue> tempKV in mSortPropertys)
                {
                    nameValue[tempKV.Key] = tempKV.Value.Value.ToString();
                }
                return(defalutValue);
            }

            if (JUtil.IsKeyValuePairGenericType(type))
            {
                Type keyType   = type.GetGenericArguments()[0];
                Type valueType = type.GetGenericArguments()[1];

                object key = null, value = null;

                if (JUtil.IsValueType(keyType))
                {
                    if (mSortPropertys.Count > 0)
                    {
                        KeyValuePair <string, JValue> tempKV = mSortPropertys[0];
                        key   = JUtil.ToObject(keyType, tempKV.Key);
                        value = tempKV.Value.ToDeserialize(valueType);
                    }
                }
                else
                {
                    if (mPropertys.ContainsKey("Key"))
                    {
                        key   = mPropertys["Key"].ToDeserialize(keyType);
                        value = mPropertys["Value"].ToDeserialize(valueType);
                    }
                }

                if (null != key)
                {
                    defalutValue = JUtil.CreateInstance(type, key, value);
                }

                return(defalutValue);
            }

            PropertyInfo[] propertyInfo = JUtil.GetSerializableProperties(type);

            if (null != propertyInfo)
            {
                for (int i = 0; i < propertyInfo.Length; ++i)
                {
                    if (propertyInfo[i].CanWrite)
                    {
                        string name = propertyInfo[i].Name;
                        if (mPropertys.ContainsKey(name))
                        {
                            propertyInfo[i].SetValue(defalutValue, mPropertys[name].ToDeserialize(propertyInfo[i].PropertyType), null);
                        }
                    }
                }
            }

            FieldInfo[] fieldInfo = JUtil.GetSerializableFields(type);

            if (null != fieldInfo)
            {
                for (int i = 0; i < fieldInfo.Length; ++i)
                {
                    string name = fieldInfo[i].Name;
                    if (mPropertys.ContainsKey(name))
                    {
                        fieldInfo[i].SetValue(defalutValue, mPropertys[name].ToDeserialize(fieldInfo[i].FieldType));
                    }
                }
            }

            return(defalutValue);
        }