/// <summary> /// Initialize a new instance of <see cref="ObjectMapper{T}"/> with /// the specified <see cref="IDataRecord"/>. /// </summary> /// <param name="template"> /// The template for the mapping. The value setters will be initialized /// according to the template. /// </param> public ObjectMapper(IDataRecord template) { var type = typeof(T); _targetConstructor = ConstructorInvokerGenerator.CreateDelegate(type); var props = type .GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(x => x.CanWrite); #if NET35 var propMap = MakeMemberMap(props.Cast <MemberInfo>()); #else var propMap = MakeMemberMap(props); #endif var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); var fieldMap = MakeMemberMap(fields); _targetMemberSetups = new List <MemberSetupInfo>(); var flags = new bool[template.FieldCount]; // all false // 3-level match: // use a strict match Func <string, string, bool> fieldNameMemberNameMatches = (f, m) => f == m; AppendMemberSetups(template, flags, propMap, fieldMap, fieldNameMemberNameMatches); // use a case-insensitive match fieldNameMemberNameMatches = (f, m) => StringComparer.OrdinalIgnoreCase.Compare(f, m) == 0; AppendMemberSetups(template, flags, propMap, fieldMap, fieldNameMemberNameMatches); // compare by the underlying name of the field fieldNameMemberNameMatches = (f, m) => StringComparer.OrdinalIgnoreCase.Compare(GetUnderlyingName(f), m) == 0; AppendMemberSetups(template, flags, propMap, fieldMap, fieldNameMemberNameMatches); }
/// <summary> /// Initialize a new instance of <see cref="ObjectMapper{T}"/> with /// the specified <see cref="IDataRecord"/>. /// </summary> /// <param name="template"> /// The template for the mapping. The value setters will be initialized /// according to the template. /// </param> public ObjectMapper(IDataRecord template) { var type = typeof(T); _targetConstructor = ConstructorInvokerGenerator.CreateDelegate(type); _targetMemberSetups = ParseMembers(type, template); _templateFieldCount = template.FieldCount; }
/// <summary> /// Initialize a new instance of <see cref="AnonymousObjectMapper{T}"/> with /// the specified <see cref="IDataRecord"/>. /// </summary> /// <param name="template">The template for the mapping.</param> public AnonymousObjectMapper(IDataRecord template) { var contructor = typeof(T).GetConstructors()[0]; _contructor = ConstructorInvokerGenerator.CreateDelegate(contructor); var fieldMap = GetFieldMap(template); var parameters = contructor.GetParameters(); _argumentLength = parameters.Length; _argumentInfos = new ArgumentInfo[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { var p = parameters[i]; var pType = p.ParameterType; var argumentInfo = new ArgumentInfo(); argumentInfo.ParamType = pType; argumentInfo.Name = p.Name; if (pType.IsValueType) { argumentInfo.DefaultValue = Activator.CreateInstance(pType); } int index; if (fieldMap.TryGetValue(p.Name, out index)) { argumentInfo.DataColIndex = index; var dataFieldType = template.GetFieldType(index); argumentInfo.NeedConvertType = !pType.IsAssignableFrom(dataFieldType); argumentInfo.IsEnum = dataFieldType.IsEnum; argumentInfo.CanBeNull = ReflectionUtils.IsNullable(p.ParameterType); } else { argumentInfo.DataColIndex = -1; } _argumentInfos[i] = argumentInfo; } }