/// <summary> /// 转指定类型的KeyValuePair<,>为JObject /// </summary> private static JObject ConvertKeyValuePairGenericToObject(object obj) { JObject res = new JObject(); Type keyType = obj.GetType().GetGenericArguments()[0]; Type valueType = obj.GetType().GetGenericArguments()[1]; object key = null, value = null; if (JUtil.GetProperty(obj.GetType(), "Key", obj, ref key)) { if (JUtil.GetProperty(obj.GetType(), "Value", obj, ref value)) { if (JUtil.IsValueType(keyType)) { res.Add(key.ToString(), ConvertToJValue(value)); } else { res.Add("Key", ConvertToJValue(key)); res.Add("Value", ConvertToJValue(value)); } } } return(res); }
/// <summary> /// 是否是键为值类型的Dictionary<,>泛型 /// </summary> public static bool IsValueKeyDictionaryGenericType(Type type) { if (type.IsGenericType) { if (type.GetGenericTypeDefinition() == typeof(Dictionary <,>) || type.GetGenericTypeDefinition() == typeof(SortedDictionary <,>) || type.GetGenericTypeDefinition() == typeof(IDictionary <,>)) { return(JUtil.IsValueType(type.GetGenericArguments()[0])); } } return(false); }
/// <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); }