/********************************************************************************* *Author: OnClick *Version: 0.0.1 *UnityVersion: 2018.3.11f1 *Date: 2019-09-08 *Description: IFramework *History: 2018.11-- *********************************************************************************/ using System.Collections.Generic; using System.IO; using System.Linq; namespace IFramework.Serialization.DataTable { /// /// 数据表读者 /// internal class DataReader : Unit, IDataReader { private List> _rows; private IDataRow _rowReader; private IDataExplainer _explainer; private TextReader _streamReader; private List _headNames; public List headNames { get { return _headNames; } } public List> rows { get { return _rows; } } /// /// Ctor /// /// 流读者 /// 行读者 /// 数据解释器 public DataReader(TextReader streamReader, IDataRow rowReader, IDataExplainer explainer) { this._explainer = explainer; this._streamReader = streamReader; this._rowReader = rowReader; _rows = new List>(); Read(_streamReader.ReadToEnd()); } /// /// Ctor /// /// 数据表字符串 /// 行读者 /// 数据解释器 public DataReader(string text, IDataRow rowReader, IDataExplainer explainer) { this._explainer = explainer; this._rowReader = rowReader; _rows = new List>(); Read(text); } private void Read(string str) { List rowValues = str.Replace("\r\n", "\n").Split('\n').ToList(); string HeadStr = rowValues[0]; rowValues.RemoveAt(0); _headNames = _rowReader.ReadHeadLine(HeadStr); if (string.IsNullOrEmpty(rowValues.Last())) rowValues.RemoveAt(rowValues.Count - 1); rowValues.ForEach((row) => { List cols = _rowReader.ReadLine(row, headNames); _rows.Add(cols); }); } /// /// 释放 /// protected override void OnDispose() { if (!disposed) { _headNames.Clear(); _rows.Clear(); if (_streamReader != null) { _streamReader.Close(); _streamReader.Dispose(); } } } /// /// 获取一张表数据 /// /// /// public List Get() { List ts = new List(); var members = DataTableTool.GetMemberInfo(typeof(T)); _rows.ForEach((cols) => { T t = _explainer.CreatInstance(cols, members); if (t != null) ts.Add(t); }); return ts; } } }