Esempio n. 1
0
        protected override Expression CreateExpressionBody(Expression source, Expression destination, CompileArgument arg)
        {
            Expression convert         = source;
            var        sourceType      = arg.SourceType;
            var        destinationType = arg.DestinationType;

            if (sourceType != destinationType)
            {
                if (sourceType.IsNullable())
                {
                    convert = Expression.Convert(convert, sourceType.GetGenericArguments()[0]);
                }
                convert = ReflectionUtils.BuildUnderlyingTypeConvertExpression(convert, sourceType, destinationType);
                if (convert.Type != destinationType)
                {
                    convert = Expression.Convert(convert, destinationType);
                }

                if (arg.MapType != MapType.Projection &&
                    (!arg.SourceType.GetTypeInfo().IsValueType || arg.SourceType.IsNullable()))
                {
                    //src == null ? default(TDestination) : convert(src)
                    var compareNull = Expression.Equal(source, Expression.Constant(null, sourceType));
                    convert = Expression.Condition(compareNull, Expression.Constant(destinationType.GetDefault(), destinationType), convert);
                }
            }

            return(convert);
        }