123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Tps_LQ_Transmitter.com
- {
- /// <summary>
- /// 一个指标项的测试配置表的信息
- /// </summary>
- public class TestParameters
- {
- /// <summary>
- /// 参数定义表
- /// </summary>
- public Dictionary<string, ParameterNode> Parameters;
- /// <summary>
- /// 原始测试表格,用于显示的表格
- /// </summary>
- public DataTable InitialTable;
- /// <summary>
- /// 执行完成后的测试结果表格,是initialTable的一个拷贝
- /// </summary>
- public DataTable TestTable;
- public TestParameters()
- {
- Parameters = new Dictionary<string, ParameterNode>();
- }
- public T GetParameter<T>(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<T>(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<T>(val, ',');
- }
- public ParameterNode GetParameter(string name)
- {
- if (string.IsNullOrEmpty(name))
- return null;
- if (this.Parameters.ContainsKey(name) == false)
- return null;
- return this.Parameters[name];
- }
- /// <summary>
- /// 重置测试结果表
- /// </summary>
- public void ResetTestTable()
- {
- TestTable = null;
- if(InitialTable != null)
- {
- TestTable = InitialTable.Copy();
- }
- }
- /// <summary>
- /// 复制一个测试指标配置表,避免重复读文件,这个文件要维护好
- /// </summary>
- /// <returns></returns>
- 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<T>(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;
- }
- }
- /// <summary>
- /// 参数内容定义
- /// </summary>
- 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();
- }
- }
- }
|