DataReader.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*********************************************************************************
  2. *Author: OnClick
  3. *Version: 0.0.1
  4. *UnityVersion: 2018.3.11f1
  5. *Date: 2019-09-08
  6. *Description: IFramework
  7. *History: 2018.11--
  8. *********************************************************************************/
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. namespace IFramework.Serialization.DataTable
  13. {
  14. /// <summary>
  15. /// 数据表读者
  16. /// </summary>
  17. internal class DataReader : Unit, IDataReader
  18. {
  19. private List<List<DataColumn>> _rows;
  20. private IDataRow _rowReader;
  21. private IDataExplainer _explainer;
  22. private TextReader _streamReader;
  23. private List<string> _headNames;
  24. public List<string> headNames { get { return _headNames; } }
  25. public List<List<DataColumn>> rows { get { return _rows; } }
  26. /// <summary>
  27. /// Ctor
  28. /// </summary>
  29. /// <param name="streamReader">流读者</param>
  30. /// <param name="rowReader">行读者</param>
  31. /// <param name="explainer">数据解释器</param>
  32. public DataReader(TextReader streamReader, IDataRow rowReader, IDataExplainer explainer)
  33. {
  34. this._explainer = explainer;
  35. this._streamReader = streamReader;
  36. this._rowReader = rowReader;
  37. _rows = new List<List<DataColumn>>();
  38. Read(_streamReader.ReadToEnd());
  39. }
  40. /// <summary>
  41. /// Ctor
  42. /// </summary>
  43. /// <param name="text">数据表字符串</param>
  44. /// <param name="rowReader">行读者</param>
  45. /// <param name="explainer">数据解释器</param>
  46. public DataReader(string text, IDataRow rowReader, IDataExplainer explainer)
  47. {
  48. this._explainer = explainer;
  49. this._rowReader = rowReader;
  50. _rows = new List<List<DataColumn>>();
  51. Read(text);
  52. }
  53. private void Read(string str)
  54. {
  55. List<string> rowValues = str.Replace("\r\n", "\n").Split('\n').ToList();
  56. string HeadStr = rowValues[0];
  57. rowValues.RemoveAt(0);
  58. _headNames = _rowReader.ReadHeadLine(HeadStr);
  59. if (string.IsNullOrEmpty(rowValues.Last())) rowValues.RemoveAt(rowValues.Count - 1);
  60. rowValues.ForEach((row) =>
  61. {
  62. List<DataColumn> cols = _rowReader.ReadLine(row, headNames);
  63. _rows.Add(cols);
  64. });
  65. }
  66. /// <summary>
  67. /// 释放
  68. /// </summary>
  69. protected override void OnDispose()
  70. {
  71. if (!disposed)
  72. {
  73. _headNames.Clear();
  74. _rows.Clear();
  75. if (_streamReader != null)
  76. {
  77. _streamReader.Close();
  78. _streamReader.Dispose();
  79. }
  80. }
  81. }
  82. /// <summary>
  83. /// 获取一张表数据
  84. /// </summary>
  85. /// <typeparam name="T"></typeparam>
  86. /// <returns></returns>
  87. public List<T> Get<T>()
  88. {
  89. List<T> ts = new List<T>();
  90. var members = DataTableTool.GetMemberInfo(typeof(T));
  91. _rows.ForEach((cols) =>
  92. {
  93. T t = _explainer.CreatInstance<T>(cols, members);
  94. if (t != null) ts.Add(t);
  95. });
  96. return ts;
  97. }
  98. }
  99. }