示例#1
0
            private static Expression CreateGetValueExpression(
                int index,
                bool nullable,
                RelationalTypeMapping typeMapping,
                Type clrType)
            {
                var getMethod = typeMapping.GetDataReaderMethod();

                var indexExpression = Expression.Constant(index);

                Expression valueExpression
                    = Expression.Call(
                          getMethod.DeclaringType != typeof(DbDataReader)
                            ? Expression.Convert(DataReaderParameter, getMethod.DeclaringType)
                            : (Expression)DataReaderParameter,
                          getMethod,
                          indexExpression);

                valueExpression = typeMapping.CustomizeDataReaderExpression(valueExpression);

                var converter = typeMapping.Converter;

                if (converter != null)
                {
                    if (valueExpression.Type != converter.ProviderClrType)
                    {
                        valueExpression = Expression.Convert(valueExpression, converter.ProviderClrType);
                    }

                    valueExpression = ReplacingExpressionVisitor.Replace(
                        converter.ConvertFromProviderExpression.Parameters.Single(),
                        valueExpression,
                        converter.ConvertFromProviderExpression.Body);
                }

                if (valueExpression.Type != clrType)
                {
                    valueExpression = Expression.Convert(valueExpression, clrType);
                }

                //var exceptionParameter
                //    = Expression.Parameter(typeof(Exception), name: "e");

                //var property = materializationInfo.Property;

                //if (detailedErrorsEnabled)
                //{
                //    var catchBlock
                //        = Expression
                //            .Catch(
                //                exceptionParameter,
                //                Expression.Call(
                //                    _throwReadValueExceptionMethod
                //                        .MakeGenericMethod(valueExpression.Type),
                //                    exceptionParameter,
                //                    Expression.Call(
                //                        dataReaderExpression,
                //                        _getFieldValueMethod.MakeGenericMethod(typeof(object)),
                //                        indexExpression),
                //                    Expression.Constant(property, typeof(IPropertyBase))));

                //    valueExpression = Expression.TryCatch(valueExpression, catchBlock);
                //}

                //if (box && valueExpression.Type.GetTypeInfo().IsValueType)
                //{
                //    valueExpression = Expression.Convert(valueExpression, typeof(object));
                //}

                if (nullable)
                {
                    valueExpression
                        = Expression.Condition(
                              Expression.Call(DataReaderParameter, _isDbNullMethod, indexExpression),
                              Expression.Default(valueExpression.Type),
                              valueExpression);
                }

                return(valueExpression);
            }
            private Expression CreateGetValueExpression(
                ParameterExpression dbDataReader,
                int index,
                bool nullable,
                RelationalTypeMapping typeMapping,
                Type clrType)
            {
                var getMethod = typeMapping.GetDataReaderMethod();

                Expression indexExpression = Expression.Constant(index);

                if (_indexMapParameter != null)
                {
                    indexExpression = Expression.ArrayIndex(_indexMapParameter, indexExpression);
                }

                Expression valueExpression
                    = Expression.Call(
                          getMethod.DeclaringType != typeof(DbDataReader)
                            ? Expression.Convert(dbDataReader, getMethod.DeclaringType)
                            : (Expression)dbDataReader,
                          getMethod,
                          indexExpression);

                if (ProjectionColumns != null)
                {
                    var columnType = valueExpression.Type;
                    if (!columnType.IsValueType ||
                        !BufferedDataReader.IsSupportedValueType(columnType))
                    {
                        columnType      = typeof(object);
                        valueExpression = Expression.Convert(valueExpression, typeof(object));
                    }

                    if (ProjectionColumns[index] == null)
                    {
                        ProjectionColumns[index] = ReaderColumn.Create(
                            columnType,
                            nullable,
                            _indexMapParameter != null ? ((ColumnExpression)_selectExpression.Projection[index].Expression).Name : null,
                            Expression.Lambda(
                                valueExpression,
                                dbDataReader,
                                _indexMapParameter ?? Expression.Parameter(typeof(int[]))).Compile());
                    }

                    if (getMethod.DeclaringType != typeof(DbDataReader))
                    {
                        valueExpression
                            = Expression.Call(
                                  dbDataReader,
                                  RelationalTypeMapping.GetDataReaderMethod(columnType),
                                  indexExpression);
                    }
                }

                valueExpression = typeMapping.CustomizeDataReaderExpression(valueExpression);

                var converter = typeMapping.Converter;

                if (converter != null)
                {
                    if (valueExpression.Type != converter.ProviderClrType)
                    {
                        valueExpression = Expression.Convert(valueExpression, converter.ProviderClrType);
                    }

                    valueExpression = ReplacingExpressionVisitor.Replace(
                        converter.ConvertFromProviderExpression.Parameters.Single(),
                        valueExpression,
                        converter.ConvertFromProviderExpression.Body);
                }

                if (valueExpression.Type != clrType)
                {
                    valueExpression = Expression.Convert(valueExpression, clrType);
                }

                //var exceptionParameter
                //    = Expression.Parameter(typeof(Exception), name: "e");

                //var property = materializationInfo.Property;

                //if (detailedErrorsEnabled)
                //{
                //    var catchBlock
                //        = Expression
                //            .Catch(
                //                exceptionParameter,
                //                Expression.Call(
                //                    _throwReadValueExceptionMethod
                //                        .MakeGenericMethod(valueExpression.Type),
                //                    exceptionParameter,
                //                    Expression.Call(
                //                        dataReaderExpression,
                //                        _getFieldValueMethod.MakeGenericMethod(typeof(object)),
                //                        indexExpression),
                //                    Expression.Constant(property, typeof(IPropertyBase))));

                //    valueExpression = Expression.TryCatch(valueExpression, catchBlock);
                //}

                //if (box && valueExpression.Type.GetTypeInfo().IsValueType)
                //{
                //    valueExpression = Expression.Convert(valueExpression, typeof(object));
                //}

                if (nullable)
                {
                    valueExpression
                        = Expression.Condition(
                              Expression.Call(dbDataReader, _isDbNullMethod, indexExpression),
                              Expression.Default(valueExpression.Type),
                              valueExpression);
                }

                return(valueExpression);
            }