public void Load(String[] strs, String[] columns = null, int startRow = 0, String sep = ",") { if (strs == null || strs.Length <= 0 || strs[0] == null || strs[0].Trim() == "") { return; } this.Clear(); PropertyDescriptorCollection pdc = PropertyObjectUtils.GetPropertyDescriptorCollection(typeof(T)); for (int i = startRow; i < strs.Length; i++) { if (strs[i] == null || strs[i] == "") { continue; } String s = strs[i].Trim(); if (s == "") { continue; } T t = TimeSeriesItemUtils.Parse <T>(strs[i], columns); if (t != null) { this.Add(t); } } }
/// <summary> /// 解析字符串 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="str"></param> /// <returns></returns> public static T Parse <T>(String str, String sep = ",", String[] columns = null) where T : ITimeSeriesItem { if (str == null || str == "") { return(default(T)); } Comparison <PropertyDescriptor> comparison = (x, y) => x.xh - y.xh; PropertyDescriptorCollection pdc = PropertyObjectUtils.GetPropertyDescriptorCollection(typeof(T)); pdc.Sort(comparison); List <PropertyDescriptor> pdList = new List <PropertyDescriptor>(); if (columns != null && columns.Length > 0) { for (int i = 0; i < columns.Length; i++) { PropertyDescriptor pd = pdc.Get(columns[i]); if (pd != null) { pdList.Add(pd); } } } else { pdList.AddRange(pdc); } String[] ss = str.Split(sep.ToCharArray()); if (ss == null || ss.Length <= 0) { return(default(T)); } T instance = (T)typeof(T).GetConstructor(new Type[0]).Invoke(null); for (int i = 0; i < ss.Length; i++) { Object value = ConvertUtils.ConvertTo(ss[i], pdList[i].Type); instance[pdList[i].Name] = value; } return(instance); }
/// <summary> /// 解析 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="str"></param> /// <returns></returns> public static T Parse <T>(String s, String[] propertyNames = null) where T : ITimeSeriesItem { if (s == null || s == "") { return(default(T)); } String[] columns = s.Split(','); if (columns == null || columns.Length <= 1 || columns[0].Trim() == "") { return(default(T)); } ; Type elementType = typeof(T); if (elementType.IsInterface) { elementType = typeof(TimeSeriesItem <>).MakeGenericType(typeof(T).GenericTypeArguments); } T insance = (T)typeof(T).Assembly.CreateInstance(elementType.FullName); PropertyDescriptorCollection pdc = PropertyObjectUtils.GetPropertyDescriptorCollection(typeof(T)); if (pdc == null)//属性描述符没有的情况 { if (!typeof(T).IsGenericType || typeof(T).GenericTypeArguments == null || typeof(T).GenericTypeArguments.Length <= 0) { return(default(T)); } if (columns[0] != null && columns[0].Contains("=")) { columns[0] = columns[0].Split('=')[1].Trim(); } if (columns[1] != null && columns[1].Contains("=")) { columns[1] = columns[1].Split('=')[1].Trim(); } insance.Date = DateUtils.Parse(columns[0]); Object v = ConvertUtils.strToObject(columns.Merge(1, columns.Length - 1), typeof(T).GenericTypeArguments[0]); insance.SetDefaultValue(v); return(insance); } for (int i = 0; i < columns.Length; i++) { if (columns[i] == null || columns[i].Trim() == "") { continue; } PropertyDescriptor pd = pdc[i]; String value = columns[i].Trim(); if (columns[i].Contains('=')) { String[] itemstr = columns[i].Split('='); String propName = itemstr[0] == null ? "" : itemstr[0].Trim(); pd = pdc.Get(propName); if (pd == null) { pd = pdc[i]; } value = itemstr[1] == null ? "" : itemstr[1].Trim(); } insance.SetValue(pd.Name, ConvertUtils.ConvertTo(value, pd.Type, pd.Format)); } return(insance); }