/// <summary> /// 转换实体对象 /// </summary> /// <typeparam name="T1">转化的实体</typeparam> /// <typeparam name="T2">转换后的实体</typeparam> /// <param name="t1"></param> /// <param name="t2"></param> /// <returns></returns> public T2 ParstModel<T1, T2>(T1 t1, T2 t2) { try { if (t1 == null) { return t2; } else { System.Reflection.PropertyInfo[] properties = t1.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (properties.Length <= 0) { return t2; } foreach (System.Reflection.PropertyInfo item in properties) { string name = item.Name; object value = item.GetValue(t1, null); if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { t2.GetType().GetProperty(name).SetValue(t2, value, null); } else { ParstModel(value, t2); } } } return t2; } catch (Exception ex) { throw ex; } } /// <summary> ///string转为数组 /// </summary> /// <param name="str">要转化的string对象</param> /// <param name="arr">返回的string数组</param> /// <returns></returns> public string[] StrSplitToarry(string str, string[] arr) { if (str.Contains(‘,‘)) { arr = str.Split(‘,‘); } else { Array.Resize(ref arr, 1); arr[0] = str; } return arr; }
转换实体对象 与string转化数组
时间: 2024-10-11 09:29:02