static LambdaExpression OptionalConversion(Type type, ExcelParameterRegistration paramReg, bool treatEmptyAsMissing, bool treatNAErrorAsMissing)
        {
            // Decide whether to return a conversion function for this parameter
            if (!paramReg.CustomAttributes.OfType <OptionalAttribute>().Any())
            {
                return(null);
            }

            var defaultAttribute = paramReg.CustomAttributes.OfType <DefaultParameterValueAttribute>().FirstOrDefault();
            var defaultValue     = defaultAttribute == null?TypeConversion.GetDefault(type) : defaultAttribute.Value;

            // var returnType = type.GetGenericArguments()[0]; // E.g. returnType is double

            // Consume the attributes
            paramReg.CustomAttributes.RemoveAll(att => att is OptionalAttribute);
            paramReg.CustomAttributes.RemoveAll(att => att is DefaultParameterValueAttribute);

            // Here's the actual conversion function
            var input = Expression.Parameter(typeof(object), "input");

            return
                (Expression.Lambda(
                     Expression.Condition(
                         MissingTest(input, treatEmptyAsMissing, treatNAErrorAsMissing),
                         Expression.Constant(defaultValue, type),
                         TypeConversion.GetConversion(input, type)),
                     input));
        }
Beispiel #2
0
 public ShimParameter(Type type, ExcelParameterRegistration reg, ParameterConversionConfiguration config)
     : this(type, reg.CustomAttributes)
 {
     // Try to find a converter for EnumeratedType
     ParameterRegistration = reg;
     PreparePropertyConverters(config, reg, ParameterConversionRegistration.GetParameterConversion);
 }
        internal static LambdaExpression NullableConversion(
            ParameterConversionConfiguration config, Type type,
            ExcelParameterRegistration paramReg, bool treatEmptyAsMissing,
            bool treatNAErrorAsMissing)
        {
            // Decide whether to return a conversion function for this parameter
            if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(Nullable <>))
            {
                return(null);
            }

            var innerType = type.GetGenericArguments()[0]; // E.g. innerType is Complex
            LambdaExpression innerTypeConversion = ParameterConversionRegistration.GetParameterConversion(config, innerType, paramReg) ??
                                                   TypeConversion.GetConversion(typeof(object), innerType);
            ParameterExpression input = innerTypeConversion.Parameters[0];
            // Here's the actual conversion function
            var result =
                Expression.Lambda(
                    Expression.Condition(
                        // if the value is missing (or possibly empty)
                        MissingTest(input, treatEmptyAsMissing, treatNAErrorAsMissing),
                        // cast null to int?
                        Expression.Constant(null, type),
                        // else convert to int, and cast that to int?
                        Expression.Convert(Expression.Invoke(innerTypeConversion, input), type)),
                    input);

            return(result);
        }
        // Should return null if there are no conversions to apply
        internal static List<LambdaExpression> GetParameterConversions(ParameterConversionConfiguration conversionConfig, Type initialParamType, ExcelParameterRegistration paramRegistration)
        {
            var appliedConversions = new List<LambdaExpression>();

            // paramReg might be modified internally by the conversions, but won't become a different object
            var paramType = initialParamType; // Might become a different type as we convert
            foreach (var paramConversion in conversionConfig.ParameterConversions)
            {
                var lambda = paramConversion.Convert(paramType, paramRegistration);
                if (lambda == null)
                    continue;

                // We got one to apply...
                // Some sanity checks
                Debug.Assert(lambda.Parameters.Count == 1);
                Debug.Assert(lambda.ReturnType == paramType || lambda.ReturnType.IsEquivalentTo(paramType));

                appliedConversions.Add(lambda);

                // Change the Parameter Type to be whatever the conversion function takes us to
                // for the next round of processing
                paramType = lambda.Parameters[0].Type;
            }

            if (appliedConversions.Count == 0)
                return null;

            return appliedConversions;
        }
        internal static LambdaExpression NullableConversion(
            ParameterConversionConfiguration config, Type type,
            ExcelParameterRegistration paramReg, bool treatEmptyAsMissing,
            bool treatNAErrorAsMissing)
        {
            // Decide whether to return a conversion function for this parameter
            if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(Nullable<>))
                return null;

            var innerType = type.GetGenericArguments()[0]; // E.g. innerType is Complex
            LambdaExpression innerTypeConversion = ParameterConversionRegistration.GetParameterConversion(config, innerType, paramReg) ??
                                                   TypeConversion.GetConversion(typeof(object), innerType);
            ParameterExpression input = innerTypeConversion.Parameters[0];
            // Here's the actual conversion function
            var result =
                Expression.Lambda(
                    Expression.Condition(
                        // if the value is missing (or possibly empty)
                        MissingTest(input, treatEmptyAsMissing, treatNAErrorAsMissing),
                        // cast null to int?
                        Expression.Constant(null, type),
                        // else convert to int, and cast that to int?
                        Expression.Convert(Expression.Invoke(innerTypeConversion, input), type)),
                    input);
            return result;
        }
Beispiel #6
0
            internal LambdaExpression Convert(Type paramType, ExcelParameterRegistration paramReg)
            {
                if (TypeFilter != null && paramType != TypeFilter)
                {
                    return(null);
                }

                return(Conversion(paramType, paramReg));
            }
        internal static LambdaExpression NullableConversion(
            IEnumerable <ParameterConversionConfiguration.ParameterConversion> parameterConversions, Type type,
            ExcelParameterRegistration paramReg, bool treatEmptyAsMissing,
            bool treatNAErrorAsMissing)
        {
            // Decide whether to return a conversion function for this parameter
            if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(Nullable <>))
            {
                return(null);
            }

            var innerType = type.GetGenericArguments()[0]; // E.g. innerType is Complex

            // Try to find a converter for innerType in the config
            ParameterConversionConfiguration.ParameterConversion innerTypeParameterConversion = null;
            if (parameterConversions != null)
            {
                innerTypeParameterConversion =
                    parameterConversions.FirstOrDefault(c => c.Convert(innerType, paramReg) != null);
            }
            ParameterExpression input = null;
            Expression          innerTypeConversion = null;

            // if we have a converter for innertype in the config, then use it. Otherwise try one of the conversions for the basic types
            if (innerTypeParameterConversion == null)
            {
                input = Expression.Parameter(typeof(object), "input");
                innerTypeConversion = TypeConversion.GetConversion(input, innerType);
            }
            else
            {
                var innerTypeParamConverter = innerTypeParameterConversion.Convert(innerType, paramReg);
                input = Expression.Parameter(innerTypeParamConverter.Parameters[0].Type, "input");
                innerTypeConversion = Expression.Invoke(innerTypeParamConverter, input);
            }
            // Here's the actual conversion function
            var result =
                Expression.Lambda(
                    Expression.Condition(
                        // if the value is missing (or possibly empty)
                        MissingTest(input, treatEmptyAsMissing, treatNAErrorAsMissing),
                        // cast null to int?
                        Expression.Constant(null, type),
                        // else convert to int, and cast that to int?
                        Expression.Convert(innerTypeConversion, type)),
                    input);

            return(result);
        }
        static LambdaExpression EnumStringConversion(Type type, ExcelParameterRegistration paramReg)
        {
            // Decide whether to return a conversion function for this parameter
            if (!type.IsEnum)
                return null;

            var input = Expression.Parameter(typeof(object), "input");
            var enumTypeParam = Expression.Parameter(typeof(Type), "enumType");
            Expression<Func<Type, object, object>> enumParse = (t, s) => EnumParse(t, s);
            var result =
                Expression.Lambda(
                    Expression.Convert(
                        Expression.Invoke(enumParse, Expression.Constant(type), input),
                        type),
                    input);
            return result;
        }
        static LambdaExpression EnumStringConversion(Type type, ExcelParameterRegistration paramReg)
        {
            // Decide whether to return a conversion function for this parameter
            if (!type.IsEnum)
            {
                return(null);
            }

            var input         = Expression.Parameter(typeof(object), "input");
            var enumTypeParam = Expression.Parameter(typeof(Type), "enumType");
            Expression <Func <Type, object, object> > enumParse = (t, s) => EnumParse(t, s);
            var result =
                Expression.Lambda(
                    Expression.Convert(
                        Expression.Invoke(enumParse, Expression.Constant(type), input),
                        type),
                    input);

            return(result);
        }
        // Implementations
        static LambdaExpression NullableConversion(Type type, ExcelParameterRegistration paramReg, bool treatEmptyAsMissing)
        {
            // Decide whether to return a conversion function for this parameter
            if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(Nullable<>))                 // E.g. type is Nullable<double>
                return null;

            var innerType = type.GetGenericArguments()[0]; // E.g. innerType is double
            // Here's the actual conversion function
            var input = Expression.Parameter(typeof(object));
            var result =
                Expression.Lambda(
                    Expression.Condition(
                // if the value is missing (or possibly empty)
                        MissingTest(input, treatEmptyAsMissing),
                // cast null to int?
                        Expression.Constant(null, type),
                // else convert to int, and cast that to int?
                        Expression.Convert(TypeConversion.GetConversion(input, innerType), type)),
                    input);
            return result;
        }
        static LambdaExpression OptionalConversion(Type type, ExcelParameterRegistration paramReg, bool treatEmptyAsMissing)
        {
            // Decide whether to return a conversion function for this parameter
            if (!paramReg.CustomAttributes.OfType<OptionalAttribute>().Any())
                return null;

            var defaultAttribute = paramReg.CustomAttributes.OfType<DefaultParameterValueAttribute>().FirstOrDefault();
            var defaultValue = defaultAttribute == null ? TypeConversion.GetDefault(type) : defaultAttribute.Value;
            // var returnType = type.GetGenericArguments()[0]; // E.g. returnType is double

            // Consume the attributes
            paramReg.CustomAttributes.RemoveAll(att => att is OptionalAttribute);
            paramReg.CustomAttributes.RemoveAll(att => att is DefaultParameterValueAttribute);

            // Here's the actual conversion function
            var input = Expression.Parameter(typeof(object));
            return
                Expression.Lambda(
                    Expression.Condition(
                        MissingTest(input, treatEmptyAsMissing),
                        Expression.Constant(defaultValue, type),
                        TypeConversion.GetConversion(input, type)),
                    input);
        }
        // Implementations
        static LambdaExpression NullableConversion(Type type, ExcelParameterRegistration paramReg, bool treatEmptyAsMissing)
        {
            // Decide whether to return a conversion function for this parameter
            if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(Nullable <>))                 // E.g. type is Nullable<double>
            {
                return(null);
            }

            var innerType = type.GetGenericArguments()[0]; // E.g. innerType is double
            // Here's the actual conversion function
            var input  = Expression.Parameter(typeof(object));
            var result =
                Expression.Lambda(
                    Expression.Condition(
                        // if the value is missing (or possibly empty)
                        MissingTest(input, treatEmptyAsMissing),
                        // cast null to int?
                        Expression.Constant(null, type),
                        // else convert to int, and cast that to int?
                        Expression.Convert(TypeConversion.GetConversion(input, innerType), type)),
                    input);

            return(result);
        }
Beispiel #13
0
        // Should return null if there are no conversions to apply
        internal static List <LambdaExpression> GetParameterConversions(ParameterConversionConfiguration conversionConfig, Type initialParamType, ExcelParameterRegistration paramRegistration)
        {
            var appliedConversions = new List <LambdaExpression>();

            // paramReg might be modified internally by the conversions, but won't become a different object
            var paramType = initialParamType; // Might become a different type as we convert

            foreach (var paramConversion in conversionConfig.ParameterConversions)
            {
                var lambda = paramConversion.Convert(paramType, paramRegistration);
                if (lambda == null)
                {
                    continue;
                }

                // We got one to apply...
                // Some sanity checks
                Debug.Assert(lambda.Parameters.Count == 1);
                Debug.Assert(lambda.ReturnType == paramType || lambda.ReturnType.IsEquivalentTo(paramType));

                appliedConversions.Add(lambda);

                // Change the Parameter Type to be whatever the conversion function takes us to
                // for the next round of processing
                paramType = lambda.Parameters[0].Type;
            }

            if (appliedConversions.Count == 0)
            {
                return(null);
            }

            return(appliedConversions);
        }
Beispiel #14
0
 internal static LambdaExpression GetParameterConversion(ParameterConversionConfiguration conversionConfig,
                                                         Type initialParamType, ExcelParameterRegistration paramRegistration)
 {
     return(ComposeLambdas(GetParameterConversions(conversionConfig, initialParamType, paramRegistration)));
 }
 internal static LambdaExpression GetParameterConversion(ParameterConversionConfiguration conversionConfig,
     Type initialParamType, ExcelParameterRegistration paramRegistration)
 {
     return ComposeLambdas(GetParameterConversions(conversionConfig, initialParamType, paramRegistration));
 }