private static void ValidateMappings <TTarget>(IDataRecord reader) { List <PropertyInfo> props = new List <PropertyInfo>(CDataMapper.GetSourceProperties(typeof(TTarget))); for (int i = 0; i < reader.FieldCount; i++) { PropertyInfo propinfo = props.Find( delegate(PropertyInfo pi) { return(pi.Name == reader.GetName(i) ? true : false); }); if (propinfo == null) { string err = string.Format("Property '{0}' of type '{1}' is missing from the type '{2}'", reader.GetName(i), reader.GetFieldType(i), typeof(TTarget).FullName); throw new Exception(err); } } }
public static IList <T> MapList <T>(DataTable dataTable) where T : new() { DataTable dt = dataTable; IList <T> list = new List <T>(); if (dt.Rows.Count > 0) { foreach (DataRow row in dt.Rows) { T obj = new T(); foreach (DataColumn col in dt.Columns) { CDataMapper.SetPropertyValue(obj, col.ColumnName, row[col]); } list.Add(obj); } } return(list); }
public static IList <TTarget> MapReader <TTarget>(string sql, CommandType CommandType, params DbParameter[] Parameters) where TTarget : new() { IDataReader reader = ExecuteReader(sql, CommandType.StoredProcedure, Parameters); ValidateMappings <TTarget>(reader); IList <TTarget> list = new List <TTarget>(); while (reader.Read()) { TTarget obj = new TTarget(); for (int i = 0; i < reader.FieldCount; i++) { if (reader.GetValue(i) != DBNull.Value) { CDataMapper.SetPropertyValue(obj, reader.GetName(i), reader.GetValue(i)); } } list.Add(obj); } reader.Close(); return(list); }
public static IList <T> MapList <T>(string xmlData) where T : new() { DataSet ds = new DataSet(); ds.ReadXml(new StringReader(xmlData)); IList <T> list = new List <T>(); if (ds.Tables.Count > 0) { DataTable dt = ds.Tables[0]; foreach (DataRow row in dt.Rows) { T obj = new T(); foreach (DataColumn col in dt.Columns) { CDataMapper.SetPropertyValue(obj, col.ColumnName, row[col]); } list.Add(obj); } } return(list); }