// covert dispatch private static object convert(Type type, Object data) { if (data.GetType() == type || type == typeof(Object)) { return(data); } // 基本类型 if (type == typeof(int)) { return(toInt(data)); } else if (type == typeof(long)) { return(toLong(data)); } else if (type == typeof(double)) { return(toDouble(data)); } else if (type == typeof(bool)) { return(toBoolean(data)); } else if (type == typeof(string)) { return(toString(data)); } // Bmob包装类型 if (type == typeof(BmobInt)) { return(new BmobInt(toInt(data))); } else if (type == typeof(BmobLong)) { return(new BmobLong(toLong(data))); } else if (type == typeof(BmobDouble)) { return(new BmobDouble(toDouble(data))); } else if (type == typeof(BmobBoolean)) { return(new BmobBoolean(toBoolean(data))); } // 复杂类型 if (typeof(IBmobWritable).IsAssignableFrom(type)) { IDictionary <String, Object> raw = (IDictionary <String, Object>)data; IBmobWritable result = (IBmobWritable)Activator.CreateInstance(type); result.readFields(new BmobInput(raw)); return(result); } else if (typeof(IBmobValue).IsAssignableFrom(type)) { IDictionary <String, Object> raw = (IDictionary <String, Object>)data; IBmobValue result = (IBmobValue)Activator.CreateInstance(type); result.Set(result); return(result); } if (typeof(IList).IsAssignableFrom(type)) { IList raw = (IList)data; IList result = (IList)Activator.CreateInstance(type); foreach (var element in raw) { var isGeneric = type.IsGenericType(); var dataType = isGeneric ? type.GetGenericArguments()[0] : null; isGeneric = isGeneric && dataType != typeof(Object); result.Add(isGeneric ? convert(dataType, element) : element); } return(result); } else if (typeof(IDictionary).IsAssignableFrom(type)) { IDictionary <String, Object> raw = (IDictionary <String, Object>)data; IDictionary result = (IDictionary)Activator.CreateInstance(type); var isGeneric = type.IsGenericType(); var vt = isGeneric ? type.GetGenericArguments()[1] : null; foreach (var entry in raw) { isGeneric = isGeneric && type != typeof(Object); result.Add(entry.Key, isGeneric ? convert(vt, entry.Value) : entry.Value); } return(result); } throw new ArgumentException("UnExcept type: " + type + ". Use Generic Type List<T>/Dictionary<K,V> instead!"); }
/// <summary> /// 添加键值对。加入已经存在的键时,会覆盖原有的值!不同与Hashtable#Add方法抛出异常的方式。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> public void Put <T>(string key, IBmobValue <T> value) { Save(real, key, value); }