/// <summary>
        /// 转object为JObject
        /// </summary>
        private static JObject ConvertToObject(object obj)
        {
            JObject res = new JObject();

            PropertyInfo[] propertys = JUtil.GetSerializableProperties(obj.GetType());

            if (null != propertys)
            {
                for (long i = 0; i < propertys.LongLength; ++i)
                {
                    try
                    {
                        if (propertys[i].CanRead)
                        {
                            res.Add(propertys[i].Name, ConvertToJValue(propertys[i].GetValue(obj, null)));
                        }
                    }
                    catch { }
                }
            }

            FieldInfo[] fields = JUtil.GetSerializableFields(obj.GetType());

            if (null != fields)
            {
                for (long i = 0; i < fields.LongLength; ++i)
                {
                    try
                    {
                        res.Add(fields[i].Name, ConvertToJValue(fields[i].GetValue(obj)));
                    }
                    catch { }
                }
            }

            return(res);
        }
Exemple #2
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);
        }