Ejemplo n.º 1
0
            public void MapTo(object o)
            {
                var t      = o.GetType();
                var schema = Mapper.GetSchema(t);

                Mapper.MapTo(DataReader, o, t, schema);
            }
Ejemplo n.º 2
0
        private T Read <T>(IDataReader rdr)
        {
            var undelyingType = Nullable.GetUnderlyingType(typeof(T));
            var typeCode      = Type.GetTypeCode(undelyingType ?? typeof(T));

            // handle primitive single-value result
            if (typeCode != TypeCode.Object || typeof(T) == typeof(object))
            {
                if (rdr.FieldCount == 1)
                {
                    return(ChangeType <T>(rdr[0], typeCode));
                }
                else if (rdr.FieldCount > 1)
                {
                    var firstFld = FirstFieldName;
                    var val      = firstFld != null ? rdr[firstFld] : rdr[0];
                    return(ChangeType <T>(val, typeCode));
                }
                else
                {
                    return(default(T));
                }
            }
            // T is a dto
            // special handling for dictionaries
            var type = typeof(T);

            if (type == typeof(IDictionary) || type == typeof(IDictionary <string, object>) || type == typeof(Dictionary <string, object>))
            {
                return((T)((object)ReadDictionary(rdr)));
            }
            // handle as poco model
            if (CustomMappingHandler != null)
            {
                var mappingContext = new DataReaderMapperContext(DtoMapper, rdr, type);
                var mapResult      = CustomMappingHandler(mappingContext);
                if (mapResult == null)
                {
                    throw new NullReferenceException("Custom mapping handler returns null");
                }
                if (!(mapResult is T))
                {
                    throw new InvalidCastException($"Custom mapping handler returns incompatible object type '{mapResult.GetType()}' (expected '{type}')");
                }
                return((T)mapResult);
            }
            return(DtoMapper.MapTo <T>(rdr));
        }