ClazzFactory.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using XRTool.Util;
  7. namespace PublicTools.XMLDataBase
  8. {
  9. /// <summary>
  10. /// 类的解析,主要做两件事,拆包/封包
  11. /// 拆包:将类的成员变量拆成Dictionary
  12. /// </summary>
  13. /// <typeparam name="T"></typeparam>
  14. public static class ClazzFactory
  15. {
  16. /// <summary>
  17. /// 给定一个成员变量,获取bi
  18. /// </summary>
  19. /// <typeparam name="T"></typeparam>
  20. /// <returns></returns>
  21. public static string GetPriKey<T>(T t, string priKey)
  22. {
  23. if (t == null)
  24. {
  25. UnityLog.LogError(typeof(T) + " is null" + priKey);
  26. }
  27. if (string.IsNullOrEmpty(priKey))
  28. {
  29. UnityLog.LogError(typeof(T) + "prikey is null:" + priKey);
  30. }
  31. var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance);
  32. if (fields == null || fields.Length < 1)
  33. {
  34. priKey=JsonConvert.SerializeObject(t.GetType().GetProperty(priKey).GetValue(t, null)).Replace("\"", "");
  35. }
  36. else
  37. {
  38. priKey = JsonConvert.SerializeObject(t.GetType().GetField(priKey).GetValue(t)).Replace("\"", "");
  39. }
  40. return priKey;
  41. }
  42. /// <summary>
  43. /// 拆包
  44. /// 使用反射和Json解析对象
  45. /// 默认解析公共成员变量(属性)
  46. /// 当不存在公共成员变量时,解析公共方法
  47. /// 二者只能存一
  48. /// 不解析Dictionary类型
  49. /// </summary>
  50. /// <typeparam name="T"></typeparam>
  51. /// <param name="t"></param>
  52. /// <returns></returns>
  53. public static Dictionary<string, string> ClazzAnalyze<T>(T t)
  54. {
  55. Dictionary<string, string> field = new Dictionary<string, string>();
  56. if (t == null)
  57. {
  58. t = default(T);
  59. }
  60. if (t != null)
  61. {
  62. ///解析成员变量
  63. var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance);
  64. bool isHas = false;
  65. ///存在成员变量时
  66. if (fields != null && fields.Length > 0)
  67. {
  68. for (int i = 0; i < fields.Length; i++)
  69. {
  70. if (fields[i].FieldType.ToString().Contains("Dictionary"))
  71. {
  72. continue;
  73. }
  74. string value = "";
  75. string key = fields[i].Name;
  76. try
  77. {
  78. value = JsonConvert.SerializeObject(t.GetType().GetField(key).GetValue(t)).Replace("\"", "");
  79. }
  80. catch (Exception ex)
  81. {
  82. ///解包错误,无法解析类型
  83. Debug.LogError(ex.ToString() + typeof(T).Name + "--" + fields[i].Name + " is error value");
  84. }
  85. field.Add(key, value);
  86. isHas = true;
  87. }
  88. }
  89. if (!isHas)
  90. {
  91. ///不存在成员变量,检测公共赋值方法
  92. var property = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  93. if (property != null && property.Length > 0)
  94. {
  95. for (int i = 0; i < property.Length; i++)
  96. {
  97. if (property[i].PropertyType.ToString().Contains("Dictionary"))
  98. {
  99. continue;
  100. }
  101. string value = "";
  102. string key = property[i].Name;
  103. try
  104. {
  105. value = JsonConvert.SerializeObject(t.GetType().GetProperty(key).GetValue(t, null)).Replace("\"", "");
  106. }
  107. catch (Exception ex)
  108. {
  109. ///解包错误,无法解析类型
  110. Debug.LogError(ex.ToString() + typeof(T).Name + "--" + property[i].Name + " is error value");
  111. }
  112. //Debug.Log(key + value);
  113. field.Add(key, value);
  114. }
  115. }
  116. }
  117. }
  118. else
  119. {
  120. UnityLog.LogError(typeof(T).Name + " is null");
  121. }
  122. return field;
  123. }
  124. /// <summary>
  125. /// 封包
  126. /// 使用反射和json封装对象
  127. /// 默认赋值公共成员变量,属性
  128. /// 当不存在可用的成员变量时,赋值公共方法
  129. /// </summary>
  130. /// <typeparam name="T"></typeparam>
  131. /// <param name="t"></param>
  132. /// <param name="field"></param>
  133. /// <returns></returns>
  134. public static T ClazzPack<T>(Dictionary<string, string> field)
  135. {
  136. T t = (T)(Assembly.GetExecutingAssembly().CreateInstance(typeof(T).Namespace + "." + typeof(T).Name));
  137. var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance);
  138. bool isHas = false;
  139. ///存在成员变量时
  140. if (fields != null && fields.Length > 0)
  141. {
  142. for (int i = 0; i < fields.Length; i++)
  143. {
  144. if (field.ContainsKey(fields[i].Name))
  145. {
  146. object tempArr;
  147. try
  148. {
  149. if (field.ContainsKey(fields[i].Name))
  150. {
  151. string value = field[fields[i].Name];
  152. ///Json对象
  153. if (value.StartsWith("{") && value.EndsWith("}"))
  154. {
  155. string realValue = "{";
  156. string nullFlag = value.Replace("{", "").Replace("}", "");
  157. string[] arr = nullFlag.Split(',');
  158. for (int j = 0; j < arr.Length; j++)
  159. {
  160. string[] tmpaar = arr[j].Split(':');
  161. string shuxing = tmpaar[0];
  162. shuxing = "\"" + shuxing + "\"";
  163. realValue += shuxing + ":" + tmpaar[1];
  164. if (j < arr.Length - 1)
  165. {
  166. realValue += ",";
  167. }
  168. }
  169. value = realValue + "}";
  170. }
  171. else if (value.StartsWith("[") && value.EndsWith("]"))
  172. {
  173. string realValue = "[";
  174. string nullFlag = value.Replace("[", "").Replace("]", "");
  175. string[] arr = nullFlag.Split(',');
  176. for (int j = 0; j < arr.Length; j++)
  177. {
  178. realValue += "\"" + arr[j] + "\"";
  179. if (j < arr.Length - 1)
  180. {
  181. realValue += ",";
  182. }
  183. }
  184. value = realValue + "]";
  185. }
  186. else
  187. {
  188. if (!value.StartsWith("\"") && !value.EndsWith("\""))
  189. {
  190. value = "\"" + value + "\"";
  191. }
  192. }
  193. //UnityLog.Log(value);
  194. if (value == "null")
  195. {
  196. tempArr = null;
  197. }
  198. else
  199. {
  200. tempArr = JsonConvert.DeserializeObject(value, fields[i].FieldType);// StringToArr(property[i].PropertyType, field[property[i].Name].Split(new char[] { '|' }));
  201. }
  202. fields[i].SetValue(t, tempArr);
  203. isHas = true;
  204. }
  205. else
  206. {
  207. UnityLog.LogError(fields[i].Name + " is null!");
  208. }
  209. }
  210. catch (Exception ex)
  211. {
  212. ///装包失败,打印出具体的错误信息
  213. UnityLog.LogError(ex.ToString() + typeof(T).Name + "--" + fields[i].Name + field[fields[i].Name] + " is error value");
  214. }
  215. }
  216. }
  217. }
  218. if (!isHas)
  219. {
  220. ///无有效的成员变量,开始解析赋值公共方法
  221. PropertyInfo[] property = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
  222. for (int i = 0; i < property.Length; i++)
  223. {
  224. if (field.ContainsKey(property[i].Name))
  225. {
  226. object tempArr;
  227. try
  228. {
  229. if (field.ContainsKey(property[i].Name))
  230. {
  231. string value = field[property[i].Name];
  232. ///Json对象
  233. if (value.StartsWith("{") && value.EndsWith("}"))
  234. {
  235. string realValue = "{";
  236. string nullFlag = value.Replace("{", "").Replace("}", "");
  237. string[] arr = nullFlag.Split(',');
  238. for (int j = 0; j < arr.Length; j++)
  239. {
  240. string[] tmpaar = arr[j].Split(':');
  241. string shuxing = tmpaar[0];
  242. shuxing = "\"" + shuxing + "\"";
  243. realValue += shuxing + ":" + tmpaar[1];
  244. if (j < arr.Length - 1)
  245. {
  246. realValue += ",";
  247. }
  248. }
  249. value = realValue + "}";
  250. }
  251. else
  252. {
  253. if (!value.StartsWith("\"") && !value.EndsWith("\""))
  254. {
  255. value = "\"" + value + "\"";
  256. }
  257. }
  258. //UnityLog.Log(value);
  259. if (value == "null")
  260. {
  261. tempArr = null;
  262. }
  263. else
  264. {
  265. tempArr = JsonConvert.DeserializeObject(value, property[i].PropertyType);// StringToArr(property[i].PropertyType, field[property[i].Name].Split(new char[] { '|' }));
  266. }
  267. property[i].SetValue(t, tempArr, null);
  268. }
  269. else
  270. {
  271. UnityLog.LogError(property[i].Name + " is null!");
  272. }
  273. }
  274. catch (Exception ex)
  275. {
  276. ///装包失败,打印出具体的错误信息
  277. UnityLog.LogError(ex.ToString() + typeof(T).Name + "--" + property[i].Name + " is error value");
  278. }
  279. }
  280. }
  281. }
  282. return t;
  283. }
  284. }
  285. }