Exemplo n.º 1
0
        /// <summary>
        /// The ICompilableTypeConverter interface states that the param Expression value must be assignable to TSource
        /// </summary>
        public Expression GetTypeConverterExpression(Expression param)
        {
            if (param == null)
            {
                throw new ArgumentNullException("param");
            }

            return(Expression.Condition(
                       Expression.Property(param, _sourceTypeIsInitialisedProperty),
                       _wrappedConverter.GetTypeConverterExpression(param),
                       Expression.Constant(default(TDest), typeof(TDest))
                       ));
        }
        /// <summary>
        /// This must return a Linq Expression that retrieves the value from SrcType.Property as TargetType - the specified "param" Expression must have a type that
        /// is assignable to SrcType. The resulting Expression will be assigned to a Lambda Expression typed as a TSourceObject to TPropertyAsRetrieved Func.
        /// </summary>
        public override Expression GetPropertyGetterExpression(Expression param)
        {
            if (param == null)
            {
                throw new ArgumentNullException("param");
            }
            if (typeof(TSourceObject) != param.Type)
            {
                throw new ArgumentException("param.NodeType must match typeparam TSourceObject");
            }

            // Get property value (from object of type TSourceObject) without conversion (this will be as type TPropertyOnSource)
            // - If value is null, return default TPropertyAsRetrieved (not applicable if a value type)
            // - Otherwise, pass through type converter (to translate from TPropertyOnSource to TPropertyAsRetrieved)
            var propertyValue        = Expression.Property(param, _propertyInfo);
            var conversionExpression = _compilableTypeConverter.GetTypeConverterExpression(propertyValue);

            if (typeof(TPropertyOnSource).IsValueType)
            {
                // If it's a value type then it's not possible for it to be null so don't do the below work
                return(conversionExpression);
            }
            if (_compilableTypeConverter.PassNullSourceValuesForProcessing)
            {
                // If _compilableTypeConverter supports passing null into it, then don't generate the condition that prevents this from happening
                return(conversionExpression);
            }
            return(Expression.Condition(
                       Expression.Equal(
                           propertyValue,
                           Expression.Constant(null)
                           ),
                       Expression.Constant(default(TPropertyAsRetrieved), typeof(TPropertyAsRetrieved)),
                       conversionExpression
                       ));
        }