Exemplo n.º 1
0
        private static IList <object> CorrectParameters(IList <object> Parameters)
        {
            if (Parameters == null)
            {
                return(null);
            }

            object[] newParameters = new object[Parameters.Count];
            try
            {
                for (var i = 0; i < Parameters.Count; i++)
                {
                    newParameters[i] = InternalTypeConverter.ToOuter(Parameters[i]);
                }
                return(newParameters);
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 2
0
        private static IList <object> CorrectParameters(IEnumerable <object> Parameters)
        {
            try
            {
                if (Parameters == null)
                {
                    return(null);
                }

                List <object> newParameters = new List <object>();
                var           i             = 0;
                foreach (var parameter in Parameters)
                {
                    newParameters[i++] = InternalTypeConverter.ToOuter(parameter);
                }
                return(newParameters);
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 3
0
        public static Boolean EvaluateValueOrMethod(
            Object Obj,
            String FieldOrMethodName,
            Int32 ParametersCount,
            DynContext DynLanContext)
        {
            Boolean seekInObject = (Obj != null && !(Obj is EmptyObject));

            Boolean wasValueFoundFromContext   = false;
            Boolean wasValueFoundInObject      = false;
            Boolean wasValueFoundInExpressions = false;

            ExpressionValue expressionValue = DynLanContext.GetValue(
                DynLanContext,
                FieldOrMethodName,
                seekInObject,
                !seekInObject,
                !seekInObject);

            if (expressionValue != null)
            {
                wasValueFoundFromContext = true;
            }

            if (seekInObject)
            {
                Boolean foundValue = false;

                Object value = GetValueFromObject(
                    Obj,
                    FieldOrMethodName,
                    ParametersCount,
                    expressionValue != null,
                    out foundValue);

                if (foundValue)
                {
                    wasValueFoundInObject = true;

                    if (value is MethodInfo)
                    {
                        OnpMethodInfo methodInfo = new OnpMethodInfo();
                        methodInfo.Obj        = Obj;
                        methodInfo.MethodName = FieldOrMethodName;
                        DynLanContext.CurrentExpressionState.PushValue(methodInfo);
                        return(false);
                    }
                    else
                    {
                        DynLanContext.CurrentExpressionState.PushValue(value);
                        return(false);
                    }
                }
            }

            Expression expression = DynLanContext.
                                    CurrentExpressionGroup.
                                    FindExpression(FieldOrMethodName, DynLanContext);

            if (expression == null)
            {
                Object value = expressionValue == null ?
                               null :
                               expressionValue.Value;

                value = InternalTypeConverter.ToInner(
                    value);

#if !IGNORE_NOT_DECLARED_VARIABLES
                if (!wasValueFoundFromContext && !wasValueFoundInObject)
                {
                    if (seekInObject)
                    {
                        throw new DynLanVariableNotFoundException("Variable " + FieldOrMethodName + " not found in object " + Obj + "!")
                              {
                                  Variable = FieldOrMethodName
                              }
                    }
                    ;

                    throw new DynLanVariableNotFoundException("Variable " + FieldOrMethodName + " not found!")
                          {
                              Variable = FieldOrMethodName
                          };
                }
#endif

                DynLanContext.CurrentExpressionState.PushValue(value);
                return(false);
            }
            else
            {
                wasValueFoundInExpressions = true;

                ExpressionState newExpressionState = new ExpressionState();
                newExpressionState.Expression = expression;

                DynLanContext.CurrentExpressionContext.Stack.Add(newExpressionState);
                return(false);
            }
        }
    }
Exemplo n.º 4
0
        ////////////////////////////////////////////////

        public static Object Do(
            OperatorType OperationType,
            Object Value1,
            Object Value2,
            Boolean ForceDecimals)
        {
            Object result = null;

            Type type1 = (Value1 == null ?
                          null : MyTypeHelper.GetNonNullableType(Value1.GetType()));

            Type type2 = (Value2 == null ?
                          null : MyTypeHelper.GetNonNullableType(Value2.GetType()));

            if (Value1 is Undefined || Value2 is Undefined)
            {
                if (OperationType == OperatorType.EQUAL)
                {
                    return(Value1 is Undefined && Value2 is Undefined);
                }
                else if (OperationType == OperatorType.NOT_EQUAL)
                {
                    return(!(Value1 is Undefined && Value2 is Undefined));
                }
                return(null);
            }

            if (type1 == null || type2 == null)
            {
                if (OperationType == OperatorType.EQUAL)
                {
                    return(type1 == null && type2 == null);
                }
                else if (OperationType == OperatorType.NOT_EQUAL)
                {
                    return(!(type1 == null && type2 == null));
                }

                return(null);
            }

            if (OperationType == OperatorType.EQUAL)
            {
                Boolean r = MyTypeHelper.IsEqualWithNumericConvert(Value1, Value2);
                return(r);
            }
            else if (OperationType == OperatorType.NOT_EQUAL)
            {
                Boolean r = !MyTypeHelper.IsEqualWithNumericConvert(Value1, Value2);
                return(r);
            }
            else if (OperationType == OperatorType.PROPERTY)
            {
                Object outValue = MyReflectionHelper.GetValue(InternalTypeConverter.ToOuter(Value1), UniConvert.ToString(Value2));
                return(InternalTypeConverter.ToInner(outValue));
            }
            else if (MyTypeHelper.IsNumeric(type1) && MyTypeHelper.IsNumeric(type2))
            {
                result = ExecuteOperatorOnNumericValues(Value1, Value2, OperationType, ForceDecimals);
            }
            else
            {
                if (type1 == typeof(TimeSpan) || type2 == typeof(TimeSpan))
                {
                    throw new DynLanInvalidOperationException("Invalid operation type " + OperationType + " (TimeSpan & TimeSpan)");
                }
                else if (OperationType == OperatorType.MULTIPLY && MyTypeHelper.IsNumeric(type1) && type2 == typeof(String))
                {
                    StringBuilder outString = new StringBuilder();
                    for (var i = 0; i < UniConvert.ToInt32(Value1); i++)
                    {
                        outString.Append(Value2);
                    }
                    return(outString.ToString());
                }
                else if (OperationType == OperatorType.MULTIPLY && MyTypeHelper.IsNumeric(type2) && type1 == typeof(String))
                {
                    StringBuilder outString = new StringBuilder();
                    for (var i = 0; i < UniConvert.ToInt32(Value2); i++)
                    {
                        outString.Append(Value1);
                    }
                    return(outString.ToString());
                }
                else if (type1 == typeof(String) || type2 == typeof(String))
                {
                    String str1          = (UniConvert.ToUniString(InternalTypeConverter.ToOuter(Value1)) ?? "");
                    String str2          = (UniConvert.ToUniString(InternalTypeConverter.ToOuter(Value2)) ?? "");
                    Int32  compareResult = str1.CompareTo(str2);

                    if (OperationType == OperatorType.GREATER)
                    {
                        result = compareResult > 0;
                    }
                    else if (OperationType == OperatorType.SMALLER)
                    {
                        result = compareResult < 0;
                    }
                    else if (OperationType == OperatorType.GREATER_OR_EQUAL)
                    {
                        result = compareResult >= 0;
                    }
                    else if (OperationType == OperatorType.SMALLER_OR_EQUAL)
                    {
                        result = compareResult <= 0;
                    }
                    else if (OperationType == OperatorType.EQUAL)
                    {
                        result = compareResult == 0;
                    }
                    else if (OperationType == OperatorType.NOT_EQUAL)
                    {
                        result = compareResult != 0;
                    }
                    else
                    {
                        return(str1 + str2);
                    }
                }
                else if (type1 == typeof(InternalDateTime) && type2 == typeof(InternalDateTime))
                {
                    var lN1 = (InternalDateTime)Value1;
                    var lN2 = (InternalDateTime)Value2;
                    if (OperationType == OperatorType.PLUS)
                    {
                        result = new InternalDateTime(lN1.Ticks + lN2.Ticks);
                    }
                    else if (OperationType == OperatorType.SUBTRACT)
                    {
                        result = new InternalDateTime(lN1.Ticks - lN2.Ticks);
                    }
                    else if (OperationType == OperatorType.GREATER)
                    {
                        result = lN1.Ticks > lN2.Ticks;
                    }
                    else if (OperationType == OperatorType.SMALLER)
                    {
                        result = lN1.Ticks < lN2.Ticks;
                    }
                    else if (OperationType == OperatorType.GREATER_OR_EQUAL)
                    {
                        result = lN1.Ticks >= lN2.Ticks;
                    }
                    else if (OperationType == OperatorType.SMALLER_OR_EQUAL)
                    {
                        result = lN1.Ticks <= lN2.Ticks;
                    }
                    else if (OperationType == OperatorType.EQUAL)
                    {
                        result = lN1.Ticks == lN2.Ticks;
                    }
                    else if (OperationType == OperatorType.NOT_EQUAL)
                    {
                        result = lN1.Ticks != lN2.Ticks;
                    }
                    else
                    {
                        throw new DynLanInvalidOperationException();
                    }
                }
                else if (MyTypeHelper.IsNumeric(type1) && type2 == typeof(InternalDateTime))
                {
                    var lN1 = UniConvert.ToDecimal(Value1);
                    var lN2 = (InternalDateTime)Value2;
                    if (OperationType == OperatorType.PLUS)
                    {
                        result = new InternalDateTime(lN2.Ticks + (Int64)(ticks_one_day * lN1));
                    }
                    else if (OperationType == OperatorType.SUBTRACT)
                    {
                        result = new InternalDateTime((Int64)(lN1 * ticks_one_day) - lN2.Ticks);
                    }
                    else if (OperationType == OperatorType.MULTIPLY)
                    {
                        result = new InternalDateTime((Int64)(lN1 * lN2.Ticks));
                    }
                    else if (OperationType == OperatorType.DIVIDE)
                    {
                        result = new InternalDateTime((Int64)(lN1 / lN2.Ticks));
                    }
                    else
                    {
                        throw new DynLanInvalidOperationException("Invalid operation type " + OperationType + " (numeric & DateTime)");
                    }
                }
                else if (type1 == typeof(InternalDateTime) && MyTypeHelper.IsNumeric(type2))
                {
                    var lN1 = (InternalDateTime)Value1;
                    var lN2 = UniConvert.ToDecimal(Value2);
                    if (OperationType == OperatorType.PLUS)
                    {
                        result = new InternalDateTime(lN1.Ticks + (Int64)(ticks_one_day * lN2));
                    }
                    else if (OperationType == OperatorType.SUBTRACT)
                    {
                        result = new InternalDateTime(lN1.Ticks - (Int64)(ticks_one_day * lN2));
                    }
                    else if (OperationType == OperatorType.MULTIPLY)
                    {
                        result = new InternalDateTime((Int64)(lN1.Ticks * lN2));
                    }
                    else if (OperationType == OperatorType.DIVIDE)
                    {
                        result = new InternalDateTime((Int64)(lN1.Ticks / lN2));
                    }
                    else
                    {
                        throw new DynLanInvalidOperationException("Invalid operation type " + OperationType + " (DateTime & numeric)");
                    }
                }
                else
                {
                    throw new DynLanInvalidOperationException("Invalid operation type " + OperationType + " (" + (type1 == null ? "null" : type1.Name) + " & " + (type2 == null ? "null" : type2.Name) + ")");
                }
            }

            return(result);
        }
Exemplo n.º 5
0
        private static ExpressionMethodResult EvaluateInlineMethod(
            Object Object,
            Object Method,
            IList <Object> MethodParameters,
            DynLanContext DynLanContext)
        {
            if (Method is OnpMethodInfo)
            {
                OnpMethodInfo methodInfo = Method as OnpMethodInfo;

                DynamicCallResult callResult = MyReflectionHelper.CallMethod(
                    methodInfo.Obj,
                    methodInfo.MethodName,
                    CorrectParameters(MethodParameters));

                if (callResult != null)
                {
                    return(new ExpressionMethodResult(callResult.Value));
                }
            }
            else if (Method is OnpActionMethodInfo)
            {
                OnpActionMethodInfo methodInfo = Method as OnpActionMethodInfo;

                if (methodInfo != null && methodInfo.Action != null)
                {
                    return(new ExpressionMethodResult(methodInfo.Action(CorrectParameters(MethodParameters))));
                }
            }
            else if (Method is ExpressionMethod)
            {
                ExpressionMethod       onpMethod = Method as ExpressionMethod;
                ExpressionMethodResult result    = null;

                if (Object is EmptyObject)
                {
                    result = onpMethod.
                             CalculateValueDelegate(
                        DynLanContext,
                        CorrectParameters(MethodParameters));
                }
                else
                {
#if !NET20
                    var tmp = new[] { Object }.Union(MethodParameters);
#else
                    var tmp = Linq2.From(new[] { Object }).Union(MethodParameters).ToArray();
#endif
                    result = onpMethod.
                             CalculateValueDelegate(
                        DynLanContext,
                        CorrectParameters(tmp));
                }

                return(result == null ?
                       new ExpressionMethodResult(null) :
                       result);
            }
            else if (Method is ExpressionMethodInfo)
            {
                ExpressionMethodInfo   onpMethodInfo = Method as ExpressionMethodInfo;
                ExpressionMethod       onpMethod     = BuildinMethods.GetByID(onpMethodInfo.ID);
                ExpressionMethodResult result        = null;

                if (onpMethod == null)
                {
                    return(new ExpressionMethodResult(result));
                }

                if (Object is EmptyObject)
                {
                    result = onpMethod.
                             CalculateValueDelegate(
                        DynLanContext,
                        CorrectParameters(MethodParameters));
                }
                else
                {
#if !NET20
                    var tmp = new[] { Object }.Union(MethodParameters);
#else
                    var tmp = Linq2.From(new[] { Object }).Union(MethodParameters).ToArray();
#endif
                    result = onpMethod.
                             CalculateValueDelegate(
                        DynLanContext,
                        CorrectParameters(tmp));
                }

                if (result != null)
                {
                    result.Value = InternalTypeConverter.ToInner(result.Value);
                }

                return(result == null ?
                       new ExpressionMethodResult(null) :
                       result);
            }
            else if (Method is ExpressionExtender)
            {
                ExpressionExtender onpExtender = Method as ExpressionExtender;

                return(new ExpressionMethodResult(
                           onpExtender.
                           CalculateValueDelegate(
                               DynLanContext,
                               Object,
                               MethodParameters)));
            }
            else if (Method is ExpressionExtenderInfo)
            {
                ExpressionExtenderInfo onpExtenderInfo = Method as ExpressionExtenderInfo;
                ExpressionExtender     onpExtender     = BuildinExtenders.GetByID(onpExtenderInfo.ID);

                if (onpExtender == null)
                {
                    return(new ExpressionMethodResult(null));
                }

                return(new ExpressionMethodResult(
                           onpExtender.
                           CalculateValueDelegate(
                               DynLanContext,
                               Object,
                               MethodParameters)));
            }
            else if (Method is Delegate)
            {
#if NETCE
                throw new NotSupportedException("Calling delegates is forbidden on wince2.0!");
#else
                Delegate m = Method as Delegate;

                DynamicCallResult callResult = MyReflectionHelper.CallMethod(
                    m.Target,
                    m.Method,
                    CorrectParameters(MethodParameters));

                if (callResult != null)
                {
                    return(new ExpressionMethodResult(callResult.Value));
                }
#endif
            }

            if (Method == null)
            {
                if (Object == null)
                {
                    throw new DynLanMethodNotFoundException("Cannot find a method to call");
                }
                else
                {
                    throw new DynLanMethodNotFoundException("Cannot find a method to call in object " + Object.GetType().Name + "");
                }
            }
            throw new DynLanUnsupportedMethodTypeException("Unsupported method type " + Method.GetType() + "!");
        }