示例#1
0
        internal static object AdjustType(Type operandType, object operandValue, Type toType)
        {
            // if no conversion required, we are done
            if (operandType == toType)
            {
                return(operandValue);
            }

            object converted;

            if (AdjustValueStandard(operandType, operandValue, toType, out converted))
            {
                return(converted);
            }

            // not a standard conversion, see if it's an implicit user defined conversions
            ValidationError error;
            MethodInfo      conversion = RuleValidation.FindImplicitConversion(operandType, toType, out error);

            if (conversion == null)
            {
                if (error != null)
                {
                    throw new RuleEvaluationException(error.ErrorText);
                }

                throw new RuleEvaluationException(
                          string.Format(CultureInfo.CurrentCulture,
                                        Messages.CastIncompatibleTypes,
                                        RuleDecompiler.DecompileType(operandType),
                                        RuleDecompiler.DecompileType(toType)));
            }

            // now we have a method, need to do the conversion S -> Sx -> Tx -> T
            Type sx = conversion.GetParameters()[0].ParameterType;
            Type tx = conversion.ReturnType;

            object intermediateResult1;

            if (AdjustValueStandard(operandType, operandValue, sx, out intermediateResult1))
            {
                // we are happy with the first conversion, so call the user's static method
                object intermediateResult2 = conversion.Invoke(null, new object[] { intermediateResult1 });
                object intermediateResult3;
                if (AdjustValueStandard(tx, intermediateResult2, toType, out intermediateResult3))
                {
                    return(intermediateResult3);
                }
            }
            throw new RuleEvaluationException(
                      string.Format(CultureInfo.CurrentCulture,
                                    Messages.CastIncompatibleTypes,
                                    RuleDecompiler.DecompileType(operandType),
                                    RuleDecompiler.DecompileType(toType)));
        }
示例#2
0
        internal static object AdjustType(Type operandType, object operandValue, Type toType)
        {
            object          obj2;
            ValidationError error;
            object          obj3;

            if (operandType == toType)
            {
                return(operandValue);
            }
            if (AdjustValueStandard(operandType, operandValue, toType, out obj2))
            {
                return(obj2);
            }
            MethodInfo info = RuleValidation.FindImplicitConversion(operandType, toType, out error);

            if (info == null)
            {
                if (error != null)
                {
                    throw new RuleEvaluationException(error.ErrorText);
                }
                throw new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, new object[] { RuleDecompiler.DecompileType(operandType), RuleDecompiler.DecompileType(toType) }));
            }
            Type parameterType = info.GetParameters()[0].ParameterType;
            Type returnType    = info.ReturnType;

            if (AdjustValueStandard(operandType, operandValue, parameterType, out obj3))
            {
                object obj5;
                object obj4 = info.Invoke(null, new object[] { obj3 });
                if (AdjustValueStandard(returnType, obj4, toType, out obj5))
                {
                    return(obj5);
                }
            }
            throw new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, new object[] { RuleDecompiler.DecompileType(operandType), RuleDecompiler.DecompileType(toType) }));
        }