TestParameters.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Tps_LQ_Transmitter.com
  8. {
  9. /// <summary>
  10. /// 一个指标项的测试配置表的信息
  11. /// </summary>
  12. public class TestParameters
  13. {
  14. /// <summary>
  15. /// 参数定义表
  16. /// </summary>
  17. public Dictionary<string, ParameterNode> Parameters;
  18. /// <summary>
  19. /// 原始测试表格,用于显示的表格
  20. /// </summary>
  21. public DataTable InitialTable;
  22. /// <summary>
  23. /// 执行完成后的测试结果表格,是initialTable的一个拷贝
  24. /// </summary>
  25. public DataTable TestTable;
  26. public TestParameters()
  27. {
  28. Parameters = new Dictionary<string, ParameterNode>();
  29. }
  30. public T GetParameter<T>(string name)
  31. {
  32. if (string.IsNullOrEmpty(name))
  33. return default(T);
  34. if (this.Parameters.ContainsKey(name) == false)
  35. return default(T);
  36. string val = this.Parameters[name].StrValue.Trim();
  37. T t = default(T);
  38. if (typeof(T) == typeof(int))
  39. {
  40. if (val.StartsWith("0x") == false)
  41. {
  42. t = (T)(object)Convert.ToInt32(val);
  43. }
  44. else
  45. {
  46. t = (T)(object)Convert.ToInt32(val, 16);
  47. }
  48. }
  49. else if (typeof(T) == typeof(string))
  50. {
  51. t = (T)(object)val;
  52. }
  53. else if (typeof(T) == typeof(uint))
  54. {
  55. if (val.StartsWith("0x") == false)
  56. {
  57. t = (T)(object)Convert.ToUInt32(val);
  58. }
  59. else
  60. {
  61. t = (T)(object)Convert.ToUInt32(val, 16);
  62. }
  63. }
  64. else if (typeof(T) == typeof(short))
  65. {
  66. if (val.StartsWith("0x") == false)
  67. {
  68. t = (T)(object)Convert.ToInt16(val);
  69. }
  70. else
  71. {
  72. t = (T)(object)Convert.ToInt16(val, 16);
  73. }
  74. }
  75. else if (typeof(T) == typeof(ushort))
  76. {
  77. if (val.StartsWith("0x") == false)
  78. {
  79. t = (T)(object)Convert.ToUInt16(val);
  80. }
  81. else
  82. {
  83. t = (T)(object)Convert.ToUInt16(val, 16);
  84. }
  85. }
  86. else if (typeof(T) == typeof(byte))
  87. {
  88. if (val.StartsWith("0x") == false)
  89. {
  90. t = (T)(object)Convert.ToByte(val);
  91. }
  92. else
  93. {
  94. t = (T)(object)Convert.ToByte(val, 16);
  95. }
  96. }
  97. else if (typeof(T) == typeof(bool))
  98. {
  99. t = (T)(object)Convert.ToBoolean(val);
  100. }
  101. else if (typeof(T) == typeof(Int64))
  102. {
  103. if (val.StartsWith("0x") == false)
  104. {
  105. t = (T)(object)Convert.ToInt64(val);
  106. }
  107. else
  108. {
  109. t = (T)(object)Convert.ToInt64(val, 16);
  110. }
  111. }
  112. else if (typeof(T) == typeof(UInt64))
  113. {
  114. if (val.StartsWith("0x") == false)
  115. {
  116. t = (T)(object)Convert.ToUInt64(val);
  117. }
  118. else
  119. {
  120. t = (T)(object)Convert.ToUInt64(val, 16);
  121. }
  122. }
  123. else if (typeof(T) == typeof(double))
  124. {
  125. t = (T)(object)Convert.ToDouble(val);
  126. }
  127. return t;
  128. }
  129. public T[] GetParameterToArray<T>(string name)
  130. {
  131. if (string.IsNullOrEmpty(name))
  132. return default(T[]);
  133. if (this.Parameters.ContainsKey(name) == false)
  134. return default(T[]);
  135. //中英文逗号转换
  136. string val = this.Parameters[name].StrValue.Replace(',', ',');
  137. return StringToArray<T>(val, ',');
  138. }
  139. public ParameterNode GetParameter(string name)
  140. {
  141. if (string.IsNullOrEmpty(name))
  142. return null;
  143. if (this.Parameters.ContainsKey(name) == false)
  144. return null;
  145. return this.Parameters[name];
  146. }
  147. /// <summary>
  148. /// 重置测试结果表
  149. /// </summary>
  150. public void ResetTestTable()
  151. {
  152. TestTable = null;
  153. if(InitialTable != null)
  154. {
  155. TestTable = InitialTable.Copy();
  156. }
  157. }
  158. /// <summary>
  159. /// 复制一个测试指标配置表,避免重复读文件,这个文件要维护好
  160. /// </summary>
  161. /// <returns></returns>
  162. public TestParameters Copy()
  163. {
  164. TestParameters newTp = new TestParameters();
  165. newTp.InitialTable = this.InitialTable.Copy();
  166. newTp.ResetTestTable();
  167. foreach (var item in Parameters)
  168. {
  169. if (item.Value != null)
  170. {
  171. newTp.Parameters.Add(item.Key, item.Value.Copy());
  172. }
  173. else
  174. {
  175. newTp.Parameters.Add(item.Key,new ParameterNode());
  176. }
  177. }
  178. return newTp;
  179. }
  180. T[] StringToArray<T>(string str, char SeparateChar)
  181. {
  182. if (str == null)
  183. return default(T[]);
  184. string[] strs = str.Split(SeparateChar);
  185. if (strs == null || strs.Length <= 0)
  186. return default(T[]);
  187. T[] t = new T[strs.Length];
  188. for (int i = 0; i < strs.Length; i++)
  189. {
  190. if (string.IsNullOrEmpty(strs[i]) == false)
  191. strs[i] = strs[i].ToLower().Trim();
  192. }
  193. if (typeof(T) == typeof(int))
  194. {
  195. for (int i = 0; i < strs.Length; i++)
  196. {
  197. if (strs[i].StartsWith("0x") == false)
  198. {
  199. t[i] = (T)(object)Convert.ToInt32(strs[i]);
  200. }
  201. else
  202. {
  203. t[i] = (T)(object)Convert.ToInt32(strs[i], 16);
  204. }
  205. }
  206. }
  207. else if (typeof(T) == typeof(string))
  208. {
  209. for (int i = 0; i < strs.Length; i++)
  210. {
  211. t[i] = (T)(object)strs[i];
  212. }
  213. }
  214. else if (typeof(T) == typeof(uint))
  215. {
  216. for (int i = 0; i < strs.Length; i++)
  217. {
  218. if (strs[i].StartsWith("0x") == false)
  219. {
  220. t[i] = (T)(object)Convert.ToUInt32(strs[i], 16);
  221. }
  222. else
  223. {
  224. t[i] = (T)(object)Convert.ToUInt32(strs[i]);
  225. }
  226. }
  227. }
  228. else if (typeof(T) == typeof(short))
  229. {
  230. for (int i = 0; i < strs.Length; i++)
  231. {
  232. if (strs[i].StartsWith("0x") == false)
  233. {
  234. t[i] = (T)(object)Convert.ToInt16(strs[i], 16);
  235. }
  236. else
  237. {
  238. t[i] = (T)(object)Convert.ToInt16(strs[i]);
  239. }
  240. }
  241. }
  242. else if (typeof(T) == typeof(ushort))
  243. {
  244. for (int i = 0; i < strs.Length; i++)
  245. {
  246. if (strs[i].StartsWith("0x") == false)
  247. {
  248. t[i] = (T)(object)Convert.ToUInt16(strs[i], 16);
  249. }
  250. else
  251. {
  252. t[i] = (T)(object)Convert.ToUInt16(strs[i]);
  253. }
  254. }
  255. }
  256. else if (typeof(T) == typeof(byte))
  257. {
  258. for (int i = 0; i < strs.Length; i++)
  259. {
  260. if (strs[i].StartsWith("0x") == false)
  261. {
  262. t[i] = (T)(object)Convert.ToByte(strs[i], 16);
  263. }
  264. else
  265. {
  266. t[i] = (T)(object)Convert.ToByte(strs[i]);
  267. }
  268. }
  269. }
  270. else if (typeof(T) == typeof(bool))
  271. {
  272. for (int i = 0; i < strs.Length; i++)
  273. {
  274. t[i] = (T)(object)Convert.ToBoolean(strs[i]);
  275. }
  276. }
  277. else if (typeof(T) == typeof(Int64))
  278. {
  279. for (int i = 0; i < strs.Length; i++)
  280. {
  281. if (strs[i].StartsWith("0x") == false)
  282. {
  283. t[i] = (T)(object)Convert.ToInt64(strs[i], 16);
  284. }
  285. else
  286. {
  287. t[i] = (T)(object)Convert.ToInt64(strs[i]);
  288. }
  289. }
  290. }
  291. else if (typeof(T) == typeof(UInt64))
  292. {
  293. for (int i = 0; i < strs.Length; i++)
  294. {
  295. if (strs[i].StartsWith("0x") == false)
  296. {
  297. t[i] = (T)(object)Convert.ToUInt64(strs[i], 16);
  298. }
  299. else
  300. {
  301. t[i] = (T)(object)Convert.ToUInt64(strs[i]);
  302. }
  303. }
  304. }
  305. else if (typeof(T) == typeof(double))
  306. {
  307. for (int i = 0; i < strs.Length; i++)
  308. {
  309. t[i] = (T)(object)Convert.ToDouble(strs[i]);
  310. }
  311. }
  312. return t;
  313. }
  314. }
  315. /// <summary>
  316. /// 参数内容定义
  317. /// </summary>
  318. public class ParameterNode
  319. {
  320. public string Name { get; set; }
  321. public string Type { get; set; }
  322. public string StrValue { get; set; }
  323. public string Mark { get; set; }
  324. public ParameterNode Copy()
  325. {
  326. return (ParameterNode)this.MemberwiseClone();
  327. }
  328. }
  329. }