/// <summary> /// DbDataReader To List /// </summary> internal static List <T> ChangeReaderToList <T>(DbDataReader reader) { Type t = typeof(T); List <T> list = new List <T>(); if (t.Name == "MDataRow") { MDataTable dt = reader; foreach (object row in dt.Rows) { list.Add((T)row); } } else if (reader != null) { #region Reader if (reader.HasRows) { Dictionary <string, Type> kv = new Dictionary <string, Type>(StringComparer.OrdinalIgnoreCase); for (int i = 0; i < reader.FieldCount; i++) { kv.Add(reader.GetName(i), reader.GetFieldType(i)); } List <PropertyInfo> pInfoList = ReflectTool.GetPropertyList(t); List <FieldInfo> fInfoList = ReflectTool.GetFieldList(t); while (reader.Read()) { T obj = Activator.CreateInstance <T>(); if (pInfoList.Count > 0) { foreach (PropertyInfo p in pInfoList)//遍历实体 { if (p.CanWrite && kv.ContainsKey(p.Name)) { object objValue = reader[p.Name]; if (objValue != null && objValue != DBNull.Value) { if (p.PropertyType != kv[p.Name]) { objValue = ChangeType(objValue, p.PropertyType);//尽量避免类型转换 } p.SetValue(obj, objValue, null); } } } } if (fInfoList.Count > 0) { foreach (FieldInfo f in fInfoList)//遍历实体 { if (kv.ContainsKey(f.Name)) { object objValue = reader[f.Name]; if (objValue != null && objValue != DBNull.Value) { if (f.FieldType != kv[f.Name]) { objValue = ChangeType(objValue, f.FieldType);//尽量避免类型转换 } f.SetValue(obj, objValue); } } } } list.Add(obj); } kv.Clear(); kv = null; } reader.Close(); reader.Dispose(); reader = null; #endregion } return(list); //return List<default(T)>; }
internal static List <T> SplitList <T>(string json, int topN, EscapeOp op) { List <T> result = new List <T>(); if (!string.IsNullOrEmpty(json)) { Type t = typeof(T); // object entity = Activator.CreateInstance(t); T entity = Activator.CreateInstance <T>(); bool hasSetValue = false; List <PropertyInfo> pInfoList = ReflectTool.GetPropertyList(t); List <FieldInfo> fInfoList = ReflectTool.GetFieldList(t); //string key = string.Empty; StringBuilder key = new StringBuilder(32); StringBuilder value = new StringBuilder(); CharState cs = new CharState(false); try { #region 核心逻辑 char c; for (int i = 0; i < json.Length; i++) { c = json[i]; if (!cs.IsKeyword(c)) //设置关键符号状态。 { if (cs.jsonStart) //Json进行中。。。 { if (cs.keyStart > 0) { key.Append(c); } else if (cs.valueStart > 0) { value.Append(c); } } else if (!cs.arrayStart)//json结束,又不是数组,则退出。 { break; } } else if (cs.childrenStart)//正常字符,值状态下。 { int temp; int length = GetValueLength(false, json, i, false, out temp);//优化后,速度快了10倍 value.Length = 0; value.Append(json.Substring(i, length)); cs.childrenStart = false; cs.valueStart = 0; cs.setDicValue = true; i = i + length - 1; } if (cs.setDicValue)//设置键值对。 { if (key.Length > 0) { string k = key.ToString(); string val = value.ToString();//.TrimEnd('\r', '\n', '\t'); bool isNull = json[i - 5] == ':' && json[i] != '"' && val.Length == 4 && val == "null"; if (isNull) { val = ""; } else { val = JsonHelper.UnEscape(val, op); } object o = val; foreach (PropertyInfo p in pInfoList) { if (String.Compare(p.Name, k, StringComparison.OrdinalIgnoreCase) == 0) { if (p.CanWrite) { if (p.PropertyType.Name != "String") { o = ConvertTool.ChangeType(val, p.PropertyType); } p.SetValue(entity, o, null); hasSetValue = true; } break; } } if (!hasSetValue && fInfoList.Count > 0) { foreach (FieldInfo f in fInfoList) { if (String.Compare(f.Name, k, StringComparison.OrdinalIgnoreCase) == 0) { if (f.FieldType.Name != "String") { o = ConvertTool.ChangeType(val, f.FieldType); } f.SetValue(entity, o); hasSetValue = true; break; } } } } cs.setDicValue = false; key.Length = 0; value.Length = 0; } if (!cs.jsonStart && hasSetValue) { result.Add(entity); if (topN > 0 && result.Count >= topN) { return(result); } if (cs.arrayStart)//处理数组。 { entity = Activator.CreateInstance <T>(); hasSetValue = false; } } } #endregion } catch (Exception err) { Log.Write(err, LogType.Error); } finally { key = null; value = null; } } return(result); }