DataExplainer.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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;
  10. using System.Collections.Generic;
  11. using System.Reflection;
  12. namespace IFramework.Serialization.DataTable
  13. {
  14. /// <summary>
  15. /// string 解释器
  16. /// </summary>
  17. public class DataExplainer : IDataExplainer
  18. {
  19. private char dot;
  20. private char quotes;
  21. /// <summary>
  22. /// ctor
  23. /// </summary>
  24. /// <param name="dot">替换逗号的字符</param>
  25. /// <param name="quotes">替换双引号的字符</param>
  26. public DataExplainer(char dot = '◞', char quotes = '◜')
  27. {
  28. this.dot = dot;
  29. this.quotes = quotes;
  30. }
  31. /// <summary>
  32. /// 创建实例
  33. /// </summary>
  34. /// <param name="type"></param>
  35. /// <returns></returns>
  36. protected object CreatInstance(Type type)
  37. {
  38. return Activator.CreateInstance(type);
  39. }
  40. /// <summary>
  41. /// 根据格子反序列化一个实例
  42. /// </summary>
  43. /// <typeparam name="T"></typeparam>
  44. /// <param name="cols"></param>
  45. /// <param name="membersDic">需要反序列化的成员</param>
  46. /// <returns></returns>
  47. public T CreatInstance<T>(List<DataColumn> cols, Dictionary<MemberInfo, string> membersDic)
  48. {
  49. object t = CreatInstance(typeof(T));
  50. foreach (var pair in membersDic)
  51. {
  52. MemberInfo m = pair.Key;
  53. string csvName = pair.Value;
  54. DataColumn column;
  55. if (m.IsDefined(typeof(DataReadColumnIndexAttribute), false))
  56. {
  57. DataReadColumnIndexAttribute attr = m.GetCustomAttributes(typeof(DataReadColumnIndexAttribute), false)[0] as DataReadColumnIndexAttribute;
  58. if (attr.index >= cols.Count)
  59. throw new Exception(string.Format("index {0} is too Large Then colums {1}", attr.index, cols.Count));
  60. column = cols[attr.index];
  61. }
  62. else
  63. {
  64. column = cols.Find((c) => { return c.headNameForRead == csvName; });
  65. }
  66. string str = FitterCsv_CreatInstance(column.value);
  67. if (m is PropertyInfo)
  68. {
  69. PropertyInfo info = m as PropertyInfo;
  70. object obj = default(object);
  71. if (StringConvert.TryConvert(str, info.PropertyType, ref obj))
  72. info.SetValue(t, obj, null);
  73. else
  74. throw new Exception(string.Format("Convert Err Type {0} Name {1} Value {2}", typeof(T), csvName, column.value));
  75. }
  76. else
  77. {
  78. FieldInfo info = m as FieldInfo;
  79. object obj = default(object);
  80. if (StringConvert.TryConvert(str, info.FieldType, ref obj))
  81. info.SetValue(t, obj);
  82. else
  83. throw new Exception(string.Format("Convert Err Type {0} Name {1} Value {2}", typeof(T), csvName, column.value));
  84. }
  85. }
  86. return (T)t;
  87. }
  88. /// <summary>
  89. /// 根据 具体类型 获取单个数据格子数据
  90. /// </summary>
  91. /// <typeparam name="T"></typeparam>
  92. /// <param name="t"></param>
  93. /// <param name="membersDic">需要序列化的成员</param>
  94. /// <returns></returns>
  95. public List<DataColumn> GetColumns<T>(T t, Dictionary<MemberInfo, string> membersDic)
  96. {
  97. List<DataColumn> columns = new List<DataColumn>();
  98. foreach (var member in membersDic)
  99. {
  100. string val = string.Empty;
  101. MemberInfo m = member.Key;
  102. if (m is PropertyInfo)
  103. {
  104. PropertyInfo info = m as PropertyInfo;
  105. val = StringConvert.ConvertToString(info.GetValue(t, null), info.PropertyType);
  106. }
  107. else
  108. {
  109. FieldInfo info = m as FieldInfo;
  110. val = StringConvert.ConvertToString(info.GetValue(t), info.FieldType);
  111. }
  112. columns.Add(new DataColumn()
  113. {
  114. value = FitterCsv_GetColum(val)
  115. });
  116. }
  117. return columns;
  118. }
  119. private string FitterCsv_GetColum(string source)
  120. {
  121. return source.Replace(StringConvert.dot, dot).Replace('\"', quotes);
  122. }
  123. private string FitterCsv_CreatInstance(string source)
  124. {
  125. return source.Replace(dot, StringConvert.dot).Replace(quotes, '\"');
  126. }
  127. }
  128. }