/** * 检测传入的元素类型 * * @param listTpye * @param o */ private void CheckObjectType(List <string> listTpye, Object o) { if (o == null) { throw new Exception("object is null"); } if (o.GetType().IsArray) { Type elementType = o.GetType().GetElementType(); listTpye.Add("list"); CheckObjectType(listTpye, BasicClassTypeUtil.CreateObject(elementType)); } else if (o is IList) { listTpye.Add("list"); IList list = (IList)o; if (list.Count > 0) { CheckObjectType(listTpye, list[0]); } else { listTpye.Add("?"); } } else if (o is IDictionary) { listTpye.Add("map"); IDictionary map = (IDictionary)o; if (map.Count > 0) { foreach (object key in map.Keys) { listTpye.Add(BasicClassTypeUtil.CS2UniType(key.GetType().ToString())); CheckObjectType(listTpye, map[key]); break; } } else { listTpye.Add("?"); listTpye.Add("?"); //throw new ArgumentException("map can not is empty"); } } else { listTpye.Add(BasicClassTypeUtil.CS2UniType(o.GetType().ToString())); } }
/** * 获取一个元素,只能用于tup版本2,如果待获取的数据为tup3,则抛异常 * @param <T> * @param name * @return * @throws ObjectCreateException */ public T Get <T>(string name) { if (_iVer == Const.PACKET_TYPE_TUP3) { throw new Exception("data is encoded by new version, please use getTarsStruct(String name,T proxy)"); } object obj = null; if (!_data.ContainsKey(name)) { return((T)obj); } else if (cachedData.ContainsKey(name)) { if (!cachedData.TryGetValue(name, out obj)) { obj = null; } return((T)obj); } else { Dictionary <string, byte[]> pair; _data.TryGetValue(name, out pair); string strBasicType = ""; string className = null; byte[] data = new byte[0]; // 找到和T类型对应的数据data foreach (KeyValuePair <string, byte[]> e in pair) { className = e.Key; data = e.Value; if (className == null || className == string.Empty) { continue; } // 比较基本类型 strBasicType = BasicClassTypeUtil.CS2UniType(typeof(T).ToString()); if (className.Length > 0 && className == strBasicType) { break; } if (strBasicType == "map" && className.Length >= 3 && className.Substring(0, 3).ToLower() == "map") { break; } if (typeof(T).IsArray && className.Length > 3 && className.Substring(0, 4).ToLower() == "list") { break; } if (strBasicType == "list" && className.Length > 3 && className.Substring(0, 4).ToLower() == "list") { break; } } try { object objtmp = GetCacheProxy <T>(className); if (objtmp == null) { return((T)objtmp); } obj = decodeData(data, objtmp); if (obj != null) { SaveDataCache(name, obj); } return((T)obj); } catch (Exception ex) { QTrace.Trace(this + " Get Exception: " + ex.Message); throw new ObjectCreateException(ex); } } }
/** * Get an element, only for tup version 2, if the data to be acquired is version tup3, * throw an exception. * @param <T> * @param name * @return * @throws ObjectCreateException */ public T Get <T>(string name) { if (_iVer == Const.TUP_VERSION_3) { throw new Exception("data is encoded by new version, please use Get(string Name, T DefaultObj)"); } object obj = null; if (!_data.ContainsKey(name)) { return((T)obj); } else if (cachedData.ContainsKey(name)) { if (!cachedData.TryGetValue(name, out obj)) { obj = null; } return((T)obj); } else { Dictionary <string, byte[]> map; _data.TryGetValue(name, out map); string strBasicType = ""; string className = null; byte[] data = new byte[0]; // Find the data corresponding to the T type. foreach (KeyValuePair <string, byte[]> pair in map) { className = pair.Key; data = pair.Value; if (className == null || className == string.Empty) { continue; } // Comparative basic type. strBasicType = BasicClassTypeUtil.CS2UniType(typeof(T).ToString()); if (className.Length > 0 && className == strBasicType) { break; } if (strBasicType == "map" && className.Length >= 3 && className.Substring(0, 3).ToLower() == "map") { break; } if (typeof(T).IsArray && className.Length > 3 && className.Substring(0, 4).ToLower() == "list") { break; } if (strBasicType == "list" && className.Length > 3 && className.Substring(0, 4).ToLower() == "list") { break; } } try { object tmpObj = GetCacheProxy <T>(className); if (tmpObj == null) { return((T)tmpObj); } obj = DecodeData(data, tmpObj); if (obj != null) { SaveDataCache(name, obj); } return((T)obj); } catch (Exception ex) { QTrace.Trace(this + " Get Exception: " + ex.Message); throw new ObjectCreateException(ex); } } }