using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Tps_LQ_Transmitter.com { /// /// 一个指标项的测试配置表的信息 /// public class TestParameters { /// /// 参数定义表 /// public Dictionary Parameters; /// /// 原始测试表格,用于显示的表格 /// public DataTable InitialTable; /// /// 执行完成后的测试结果表格,是initialTable的一个拷贝 /// public DataTable TestTable; public TestParameters() { Parameters = new Dictionary(); } public T GetParameter(string name) { if (string.IsNullOrEmpty(name)) return default(T); if (this.Parameters.ContainsKey(name) == false) return default(T); string val = this.Parameters[name].StrValue.Trim(); T t = default(T); if (typeof(T) == typeof(int)) { if (val.StartsWith("0x") == false) { t = (T)(object)Convert.ToInt32(val); } else { t = (T)(object)Convert.ToInt32(val, 16); } } else if (typeof(T) == typeof(string)) { t = (T)(object)val; } else if (typeof(T) == typeof(uint)) { if (val.StartsWith("0x") == false) { t = (T)(object)Convert.ToUInt32(val); } else { t = (T)(object)Convert.ToUInt32(val, 16); } } else if (typeof(T) == typeof(short)) { if (val.StartsWith("0x") == false) { t = (T)(object)Convert.ToInt16(val); } else { t = (T)(object)Convert.ToInt16(val, 16); } } else if (typeof(T) == typeof(ushort)) { if (val.StartsWith("0x") == false) { t = (T)(object)Convert.ToUInt16(val); } else { t = (T)(object)Convert.ToUInt16(val, 16); } } else if (typeof(T) == typeof(byte)) { if (val.StartsWith("0x") == false) { t = (T)(object)Convert.ToByte(val); } else { t = (T)(object)Convert.ToByte(val, 16); } } else if (typeof(T) == typeof(bool)) { t = (T)(object)Convert.ToBoolean(val); } else if (typeof(T) == typeof(Int64)) { if (val.StartsWith("0x") == false) { t = (T)(object)Convert.ToInt64(val); } else { t = (T)(object)Convert.ToInt64(val, 16); } } else if (typeof(T) == typeof(UInt64)) { if (val.StartsWith("0x") == false) { t = (T)(object)Convert.ToUInt64(val); } else { t = (T)(object)Convert.ToUInt64(val, 16); } } else if (typeof(T) == typeof(double)) { t = (T)(object)Convert.ToDouble(val); } return t; } public T[] GetParameterToArray(string name) { if (string.IsNullOrEmpty(name)) return default(T[]); if (this.Parameters.ContainsKey(name) == false) return default(T[]); //中英文逗号转换 string val = this.Parameters[name].StrValue.Replace(',', ','); return StringToArray(val, ','); } public ParameterNode GetParameter(string name) { if (string.IsNullOrEmpty(name)) return null; if (this.Parameters.ContainsKey(name) == false) return null; return this.Parameters[name]; } /// /// 重置测试结果表 /// public void ResetTestTable() { TestTable = null; if(InitialTable != null) { TestTable = InitialTable.Copy(); } } /// /// 复制一个测试指标配置表,避免重复读文件,这个文件要维护好 /// /// public TestParameters Copy() { TestParameters newTp = new TestParameters(); newTp.InitialTable = this.InitialTable.Copy(); newTp.ResetTestTable(); foreach (var item in Parameters) { if (item.Value != null) { newTp.Parameters.Add(item.Key, item.Value.Copy()); } else { newTp.Parameters.Add(item.Key,new ParameterNode()); } } return newTp; } T[] StringToArray(string str, char SeparateChar) { if (str == null) return default(T[]); string[] strs = str.Split(SeparateChar); if (strs == null || strs.Length <= 0) return default(T[]); T[] t = new T[strs.Length]; for (int i = 0; i < strs.Length; i++) { if (string.IsNullOrEmpty(strs[i]) == false) strs[i] = strs[i].ToLower().Trim(); } if (typeof(T) == typeof(int)) { for (int i = 0; i < strs.Length; i++) { if (strs[i].StartsWith("0x") == false) { t[i] = (T)(object)Convert.ToInt32(strs[i]); } else { t[i] = (T)(object)Convert.ToInt32(strs[i], 16); } } } else if (typeof(T) == typeof(string)) { for (int i = 0; i < strs.Length; i++) { t[i] = (T)(object)strs[i]; } } else if (typeof(T) == typeof(uint)) { for (int i = 0; i < strs.Length; i++) { if (strs[i].StartsWith("0x") == false) { t[i] = (T)(object)Convert.ToUInt32(strs[i], 16); } else { t[i] = (T)(object)Convert.ToUInt32(strs[i]); } } } else if (typeof(T) == typeof(short)) { for (int i = 0; i < strs.Length; i++) { if (strs[i].StartsWith("0x") == false) { t[i] = (T)(object)Convert.ToInt16(strs[i], 16); } else { t[i] = (T)(object)Convert.ToInt16(strs[i]); } } } else if (typeof(T) == typeof(ushort)) { for (int i = 0; i < strs.Length; i++) { if (strs[i].StartsWith("0x") == false) { t[i] = (T)(object)Convert.ToUInt16(strs[i], 16); } else { t[i] = (T)(object)Convert.ToUInt16(strs[i]); } } } else if (typeof(T) == typeof(byte)) { for (int i = 0; i < strs.Length; i++) { if (strs[i].StartsWith("0x") == false) { t[i] = (T)(object)Convert.ToByte(strs[i], 16); } else { t[i] = (T)(object)Convert.ToByte(strs[i]); } } } else if (typeof(T) == typeof(bool)) { for (int i = 0; i < strs.Length; i++) { t[i] = (T)(object)Convert.ToBoolean(strs[i]); } } else if (typeof(T) == typeof(Int64)) { for (int i = 0; i < strs.Length; i++) { if (strs[i].StartsWith("0x") == false) { t[i] = (T)(object)Convert.ToInt64(strs[i], 16); } else { t[i] = (T)(object)Convert.ToInt64(strs[i]); } } } else if (typeof(T) == typeof(UInt64)) { for (int i = 0; i < strs.Length; i++) { if (strs[i].StartsWith("0x") == false) { t[i] = (T)(object)Convert.ToUInt64(strs[i], 16); } else { t[i] = (T)(object)Convert.ToUInt64(strs[i]); } } } else if (typeof(T) == typeof(double)) { for (int i = 0; i < strs.Length; i++) { t[i] = (T)(object)Convert.ToDouble(strs[i]); } } return t; } } /// /// 参数内容定义 /// public class ParameterNode { public string Name { get; set; } public string Type { get; set; } public string StrValue { get; set; } public string Mark { get; set; } public ParameterNode Copy() { return (ParameterNode)this.MemberwiseClone(); } } }