using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Data; using System.Xml; using System.IO; namespace SiaSun.LMS.Common { public class CloneObjectValues { #region ------T-T,T-DataRow,DataRow-T,DataRow-DataRow复制 /// /// 将T1实例的值复制到T2实例 /// public T2 CloneModelValue(T1 model_T1, T2 model_T2,IDictionary dicPairValue) { if (model_T1 != null && model_T2 != null) { PropertyInfo[] arPropertyInfo_T1 = model_T1.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); PropertyInfo[] arPropertyInfo_T2 = model_T2.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pInfo_T2 in arPropertyInfo_T2) { //判断是否存在值对 if (dicPairValue != null && (dicPairValue.ContainsKey(pInfo_T2.Name))) { pInfo_T2.SetValue(model_T2, Convert.ChangeType(dicPairValue[pInfo_T2.Name], pInfo_T2.PropertyType), null); } else { //判断是否存在于T1中 if (arPropertyInfo_T1.Count(r => r.Name == pInfo_T2.Name && r.PropertyType.ToString() == pInfo_T2.PropertyType.ToString()) > 0) { //获得T1的属性 PropertyInfo pInfo_T1 = arPropertyInfo_T1.Single(r => r.Name == pInfo_T2.Name && r.PropertyType.ToString() == pInfo_T2.PropertyType.ToString()); //获得属性值 object objValue_T1 = pInfo_T1.GetValue(model_T1, null); //设置属性值 pInfo_T2.SetValue(model_T2, Convert.ChangeType(objValue_T1, pInfo_T2.PropertyType), null); } } } } return model_T2; } /// /// 从DataRow中读取值付给实例 /// public T CloneModelValue(DataRow row, T model_T, IDictionary dicPairValue) { if (model_T != null) { //获得所有列集合 DataColumn[] arColumn = row.Table.Columns.Cast().ToArray(); PropertyInfo[] arPropertyInfo_T = model_T.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pInfo_T in arPropertyInfo_T) { //判断是否存在值对 if (dicPairValue != null && (dicPairValue.ContainsKey(pInfo_T.Name))) { pInfo_T.SetValue(model_T, Convert.ChangeType(dicPairValue[pInfo_T.Name], pInfo_T.PropertyType), null); } else { //判断是否存在该属性 if (arColumn.Count(r => r.ColumnName.ToLower() == pInfo_T.Name.ToLower()) > 0) { DataColumn col = arColumn.Single(r => r.ColumnName == pInfo_T.Name); if (!row.IsNull(col)) { //设置属性值 pInfo_T.SetValue(model_T, Convert.ChangeType(row[col], pInfo_T.PropertyType), null); } } } } } return model_T; } /// /// 从实例T中复制到DataRow中 /// public DataRow CloneDataRowValue(T model_T, DataRow row, IDictionary dicPairValue) { if (model_T != null) { //获得所有列集合 DataColumn[] arColumn = row.Table.Columns.Cast().ToArray(); PropertyInfo[] arPropertyInfo_T = model_T.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (DataColumn col in arColumn) { //设置默认值 row[col] = col.DefaultValue; //判断是否存在值对 if (dicPairValue != null && (dicPairValue.ContainsKey(col.ColumnName))) { row[col] = dicPairValue[col.ColumnName]; } else { //判断属性是否存在 if (arPropertyInfo_T.Count(r => r.Name.ToLower() == col.ColumnName.ToLower()) > 0) { PropertyInfo pInfo = arPropertyInfo_T.Single(r => r.Name == col.ColumnName); //设置行值 row[col] = pInfo.GetValue(model_T, null); } } } } return row; } /// /// 从实例T中复制到DataRow中 /// /// public DataRow CloneDataRowValue(DataRow row_T1, DataRow row_T2, IDictionary dicPairValue) { if (row_T1 != null && row_T2 != null) { //获得所有列集合 DataColumn[] arColumn_T1 = row_T1.Table.Columns.Cast().ToArray(); DataColumn[] arColumn_T2 = row_T2.Table.Columns.Cast().ToArray(); foreach (DataColumn col_T2 in arColumn_T2) { //设置默认值 row_T2[col_T2] = col_T2.DefaultValue; //判断是否存在值对 if (dicPairValue != null && (dicPairValue.ContainsKey(col_T2.ColumnName))) { row_T2[col_T2] = dicPairValue[col_T2.ColumnName]; } else { //判断属性是否存在 if (arColumn_T1.Count(r => r.ColumnName.ToLower() == col_T2.ColumnName.ToLower()) > 0) { //设置行值 row_T2[col_T2] = row_T1[col_T2.ColumnName]; } } } } return row_T2; } #endregion /// /// 根据数据源获得列表 /// /// public List GetListFromDataTable(DataTable tableSource,Dictionary dicDefaultValue) { List list = new List(); foreach (DataRow row in tableSource.Rows) { T t = Activator.CreateInstance(); t = this.CloneModelValue(row, t, dicDefaultValue); list.Add(t); } return list; } /// /// 根据数据源获得列表 /// /// public List GetListFromDataTable(DataRowView[] drvArray, Dictionary dicDefaultValue) { List list = new List(); foreach (DataRowView row in drvArray) { T t = Activator.CreateInstance(); t = this.CloneModelValue(row.Row, t, dicDefaultValue); list.Add(t); } return list; } #region ------XML与DataTable转换 /// /// 根据XML获得DataSet /// /// public DataTable GetDataTableFromXml(string XmlContent) { StringReader StrStream = null; XmlTextReader Xmlrdr = null; try { DataSet ds = new DataSet(); //读取字符串中的信息 StrStream = new StringReader(XmlContent); //获取StrStream中的数据 Xmlrdr = new XmlTextReader(StrStream); //ds获取Xmlrdr中的数据 ds.ReadXml(Xmlrdr); return (ds.Tables.Count == 0 ? null : ds.Tables[0]); } catch (Exception e) { throw e; } finally { //释放资源 if (Xmlrdr != null) { Xmlrdr.Close(); StrStream.Close(); StrStream.Dispose(); } } } /// /// 根据DataTable获得Xml /// public string GetXmlFromDataTable(DataTable tableXml) { if (tableXml != null) { MemoryStream ms = null; XmlTextWriter XmlWt = null; try { ms = new MemoryStream(); //根据ms实例化XmlWt XmlWt = new XmlTextWriter(ms, Encoding.Unicode); //获取ds中的数据 tableXml.WriteXml(XmlWt); int count = (int)ms.Length; byte[] temp = new byte[count]; ms.Seek(0, SeekOrigin.Begin); ms.Read(temp, 0, count); //返回Unicode编码的文本 UnicodeEncoding ucode = new UnicodeEncoding(); string returnValue = ucode.GetString(temp).Trim(); return returnValue; } catch (System.Exception ex) { throw ex; } finally { //释放资源 if (XmlWt != null) { XmlWt.Close(); ms.Close(); ms.Dispose(); } } } else { return ""; } } #endregion } }