public class Client { public int? nullableAge; [MappingMemberAttribute("age")] public int? NullableAge { get { return nullableAge; } set { nullableAge = value; } } [MappingMemberAttribute("nom")] public string nom; [MappingMemberAttribute("prenom", IgnoreIfNull = true)] public string prenom; }
Mapper<Client> ClientMapper = new Mapper<Client>(); ... IDataReader reader = cmd.ExecuteReader(); List<Client> result = ClientMapper.ExtractListFromIDataReader(reader);
dynamicMethodCreateCollectionFromDataReader = new DynamicMethod("CreateCollectionFromDataReader", typeof(void), new Type[] { typeof(ICollection<T>), typeof(IDataReader), typeof(int[]) }, typeof(T));
static Mapper() { List<MappedMember> entryList = new List<MappedMember>(); ExtractMappedFields(entryList); ExtractMappedProperties(entryList); classEntryToMapArray = entryList.ToArray(); GenerateCompiledMapper(); } private static void ExtractMappedProperties(List<MappedMember> entryList) { PropertyInfo[] properties = typeof(T).GetProperties(); foreach (PropertyInfo property in properties) { if (!property.CanWrite) continue; MappingMemberAttribute[] mappers = (MappingMemberAttribute[]) property.GetCustomAttributes(typeof(MappingMemberAttribute), false); if (mappers.Length == 0) { continue; } string mappingName = mappers[0].FieldName; MappedMember ce = new MappedMember(property.PropertyType, property, mappingName); ce.IgnoreIfNull = mappers[0].IgnoreIfNull; entryList.Add(ce); } }
private void MapIDataReader(IDataReader reader,ICollection<T> result) { int[] ordinalArray = new int[classEntryToMapArray.Length]; for (int i = 0; i < classEntryToMapArray.Length; ++i) { MappedMember classEntry = classEntryToMapArray[i]; int ordinal = reader.GetOrdinal(classEntry.MappingName); if (ordinal < 0) throw new Exception("Can't map " + classEntry.MappingName); ordinalArray[i] = ordinal; } createCollectionFromDataReaderDelegate(result, reader, ordinalArray); }