ClazzFactory.cs 13 KB

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