Ejemplo n.º 1
0
        /// <summary>
        /// Kopiuje wartości z datareader'a do klasy
        /// </summary>
        public static void MatchTo <T>(
            this MyDbReader <T> DataReader,
            Boolean CaseSensitive,
            T Item)
        {
            //var lProperties = typeof(T).GetProperties();
            //var lFields = typeof(T).GetFields();

            if (!DataReader.R.IsClosed)
            {
                for (int i = 0; i < DataReader.Count; i++)
                {
                    if (DataReader.DestTypes.ContainsKey(i))
                    {
                        var setter = DataReader.DestSetters[i];
                        if (setter != null)
                        {
                            setter.SetValue(
                                Item,
                                MyTypeHelper.ConvertTo(
                                    DataReader.R.Get <Object>(i),
                                    DataReader.DestTypes[i]),
                                null);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        //////////////////////////////

        public static String ToString(Object Object)
        {
            Type objectType = Object != null?Object.GetType() : null;

            if (Object == null)
            {
                return("NULL");
            }
            else if (MyTypeHelper.IsDateTime(objectType))
            {
                return("todatetime('" + UniConvert.ToUniString(Object) + "')");
            }
            else if (objectType == typeof(InternalDateTime))
            {
                DateTime?dateTime = objectType == null ?
                                    null :
                                    (DateTime?)new DateTime(((InternalDateTime)Object).Ticks);

                return("todatetime('" + UniConvert.ToUniString(dateTime) + "')");
            }
            else if (MyTypeHelper.IsNumeric(objectType))
            {
                return(UniConvert.ToUniString(Object));
            }
            else if (MyTypeHelper.IsString(objectType))
            {
                return("'" + Object.ToString().Replace("'", "\\'") + "'");
            }
            else
            {
                throw new NotSupportedException("Objects of type " + objectType.Name + " are not supported in expressions");
            }
        }
Ejemplo n.º 3
0
        public static string GetSqlType(Type csType)
        {
            if (csType != null)
            {
                if (MyTypeHelper.IsInteger(csType))
                {
                    return("int");
                }

                else if (MyTypeHelper.IsNumeric(csType))
                {
                    return("real");
                }

                else if (MyTypeHelper.IsDateTime(csType))
                {
                    return("datetime");
                }

                else if (MyTypeHelper.IsTimeSpan(csType))
                {
                    return("datetime");
                }
            }

            return("nvarchar(max)");
        }
Ejemplo n.º 4
0
        private void BuildCreateTableScriptForVariable(MyQuery Query, String TableName, Object Value)
        {
            if (MyTypeHelper.IsPrimitive(Value))
            {
            }

            else if (Value is IDictionary <string, object> dict)
            {
                Query.Append("declare @").Append(TableName).Append(" table (");
                Int32 index = 0;
                foreach (var keyAndValue in dict)
                {
                    if (index > 0)
                    {
                        Query.Append(", ");
                    }
                    Query.Append(keyAndValue.Key).Append(" ").Append(TSqlHelper.GetSqlType(keyAndValue.Value?.GetType()));
                    index++;
                }
                Query.Append(");");
            }

            else if (Value is IList)
            {
            }

            else if (Value.GetType().IsClass)
            {
                BuildCreateTableScriptForVariable(Query, TableName, ReflectionHelper.ToDictionary(Value));
            }
        }
Ejemplo n.º 5
0
 public static Object ToInner(Object Value)
 {
     if (Value != null && MyTypeHelper.IsDateTime(Value.GetType()))
     {
         return(new InternalDateTime(((DateTime)Value).Ticks));
     }
     return(Value);
 }
Ejemplo n.º 6
0
        private void BuildScriptForVariable(MyQuery Query, String Name, Object Value, String ParentName = null)
        {
            string name = string.IsNullOrEmpty(ParentName) ? Name : (ParentName + "_" + Name);

            if (MyTypeHelper.IsPrimitive(Value))
            {
                Query.Append($"declare @{name} {TSqlHelper.GetSqlType(Value?.GetType())};\n");
                Query.Append($"set @{name} = {{0}};\n", Value);
            }

            else if (Value is IDictionary <string, object> dict)
            {
                if (string.IsNullOrEmpty(ParentName))
                {
                    foreach (var keyAndValue in dict)
                    {
                        BuildScriptForVariable(Query, keyAndValue.Key, keyAndValue.Value, Name);
                    }
                }
                else
                {
                    BuildCreateTableScriptForVariable(Query, name, dict);
                    BuildInsertIntoTableScriptForVariable(Query, name, dict);
                }
                //BuildCreateTableScriptForVariable(Query, name, dict);
                // BuildInsertIntoTableScriptForVariable(Query, name, dict);
            }

            else if (Value is IList list)
            {
                var firstValue = ReflectionHelper.ToDictionary(list.Count > 0 ? list[0] : null);
                BuildCreateTableScriptForVariable(Query, name, firstValue);
                foreach (var item in list)
                {
                    BuildInsertIntoTableScriptForVariable(Query, name, item);
                }
            }

            else if (Value.GetType().IsClass)
            {
                var classAsDict = ReflectionHelper.ToDictionary(Value);
                if (string.IsNullOrEmpty(ParentName))
                {
                    foreach (var keyAndValue in classAsDict)
                    {
                        BuildScriptForVariable(Query, keyAndValue.Key, keyAndValue.Value, Name);
                    }
                }
                else
                {
                    BuildCreateTableScriptForVariable(Query, name, classAsDict);
                    BuildInsertIntoTableScriptForVariable(Query, name, classAsDict);
                }
            }
        }
Ejemplo n.º 7
0
 private static object ExecuteOperatorOnNumericValues(Object Value1, Object Value2, OperatorType OperationType, Boolean ForceDecimals)
 {
     if (ForceDecimals || MyTypeHelper.IsFloatNumeric(Value1) || MyTypeHelper.IsFloatNumeric(Value2))
     {
         return(ExecuteOperatorOnDecimals(Value1, Value2, OperationType));
     }
     else
     {
         return(ExecuteOperatorOnIntegers(Value1, Value2, OperationType));
     }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// pobiera wartość z datareader'a i konwertuje ją
 /// </summary>
 public static T Get <T>(this IDataReader Reader, String FieldName)
 {
     if (Reader != null)
     {
         //Object lValue = Reader[FieldName];
         var    lPoz   = Reader.GetOrdinal(FieldName);
         Object lValue = Reader.GetValue(lPoz);
         if (lValue == null || lValue == DBNull.Value)
         {
             return(default(T));
         }
         else
         {
             return(MyTypeHelper.ConvertTo <T>(lValue));
         }
     }
     return(default(T));
 }
Ejemplo n.º 9
0
        public static IList <T> SelectScalars <T>(
            this DbConnection Connection,
            String SqlStatement,
            Int32 ColumnIndex = 0)
        {
            List <T> outList = new List <T>();

            ExecuteReader(
                Connection,
                SqlStatement,
                false,
                true,
                (r) =>
            {
                T value = (T)MyTypeHelper.ConvertTo(r.GetValue(ColumnIndex), typeof(T));
                outList.Add(value);
            });
            return(outList);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// pobiera wartość z datareader'a i konwertuje ją
        /// </summary>
        public static T Get <T>(
            this IDataReader Reader,
            Int32 Index)
        {
            if (Reader != null)
            {
                Object lValue = Reader.FieldCount > Index?Reader.GetValue(Index) : null;

                if (lValue == null || lValue == DBNull.Value)
                {
                    return(default(T));
                }
                else
                {
                    return(MyTypeHelper.ConvertTo <T>(lValue));
                }
            }
            return(default(T));
        }
Ejemplo n.º 11
0
        private void BuildInsertIntoTableScriptForVariable(MyQuery Query, String TableName, Object Value)
        {
            if (MyTypeHelper.IsPrimitive(Value))
            {
            }

            else if (Value is IDictionary <string, object> dict)
            {
                Query.Append("insert into @").Append(TableName).Append(" (");
                Int32 index = 0;
                foreach (var keyAndValue in dict)
                {
                    if (index > 0)
                    {
                        Query.Append(", ");
                    }
                    Query.Append(keyAndValue.Key);
                    index++;
                }
                Query.Append(") values (");
                index = 0;
                foreach (var keyAndValue in dict)
                {
                    if (index > 0)
                    {
                        Query.Append(", ");
                    }
                    Query.AppendVal(keyAndValue.Value);
                    index++;
                }
                Query.Append(");");
            }

            else if (Value is IList)
            {
            }

            else if (Value.GetType().IsClass)
            {
                BuildInsertIntoTableScriptForVariable(Query, TableName, ReflectionHelper.ToDictionary(Value));
            }
        }
Ejemplo n.º 12
0
        ////////////////////////////////////////////////////////////////////////

        public static Object Execute(PainContext EvaluateContext, Object obj, IList <Object> Parameters)
        {
            Object Collection = obj;
            Object value      = Parameters != null && Parameters.Count > 0 ? Parameters[0] : null;
            Object Key        = Parameters != null && Parameters.Count > 1 ? Parameters[1] : null;

            if (Collection == null)
            {
                return(null);
            }

            if (Collection is PainObject)
            {
                PainObject painObj = Collection as PainObject;

                String finalKey = (String)(Key.GetType() == typeof(String) ? Key :
                                           Convert.ChangeType(Key, typeof(String), System.Globalization.CultureInfo.InvariantCulture));

                Object finValue = value == null ? null : (value.GetType() == typeof(Object) ? value :
                                                          Convert.ChangeType(value, typeof(Object), System.Globalization.CultureInfo.InvariantCulture));

                painObj[finalKey] = finValue;

                return(value);
            }

            if (Collection is IDictionary)
            {
                IDictionary dict = (IDictionary)Collection;

                Type[] arguments = dict.GetType().GetGenericArguments();
                Type   keyType   = arguments[0];
                Type   valueType = arguments[1];

                Object finalKey = Key.GetType() == keyType ? Key :
                                  Convert.ChangeType(Key, keyType, System.Globalization.CultureInfo.InvariantCulture);

                Object finValue = value == null ? null : (value.GetType() == valueType ? value :
                                                          Convert.ChangeType(value, valueType, System.Globalization.CultureInfo.InvariantCulture));

                lock (dict)
                {
                    dict.Remove(finalKey);
                    dict.Add(finalKey, finValue);
                }

                return(value);
            }

            if (Collection is IList)
            {
                Int32?index = UniConvert.ToInt32N(Key);
                if (index == null || index < 0)
                {
                    return(null);
                }

                IList list = (IList)Collection;
                if (index >= list.Count)
                {
                    return(null);
                }

                Type listType = MyTypeHelper.GetListType(list);

                Object finValue = value == null ? null : (value.GetType() == listType ? value :
                                                          Convert.ChangeType(value, listType, System.Globalization.CultureInfo.InvariantCulture));

                list[index.Value] = finValue;

                return(value);
            }

            return(null);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
0
        ////////////////////////////////////////////////////////////////////

        private static IEnumerable <DynMethod> BuildMethods()
        {
            yield return(new DynMethod()
            {
                Names = new[] { MethodSetValue.Name },
                Body = MethodSetValue.Execute
            });

            /*yield return new ExpressionMethod()
             * {
             *  OperationNames = new[] { "check" },
             *  CalculateValueDelegate = (DynLanContext, Parameters) =>
             *  {
             *      Object condition = Parameters != null && Parameters.Count > 0 ? Parameters[0] : null;
             *      Object ifTrue = Parameters != null && Parameters.Count > 1 ? Parameters[1] : null;
             *      Object ifFalse = Parameters != null && Parameters.Count > 2 ? Parameters[2] : null;
             *
             *      if (condition != null)
             *      {
             *          Type conditionType = condition.GetType();
             *
             *          if ((MyTypeHelper.IsNumeric(conditionType) && Convert.ToDecimal(condition) > 0) ||
             *              (conditionType == typeof(String) && Convert.ToString(condition).Length > 0))
             *          {
             *              return new ExpressionMethodResult(ifTrue);
             *          }
             *          else
             *          {
             *              return new ExpressionMethodResult(ifFalse);
             *          }
             *      }
             *
             *      return new ExpressionMethodResult(null);
             *  }
             * };*/
            yield return(new DynMethod()
            {
                Names = new[] { "getdatetime" },
                Body = (DynLanContext, Parameters) =>
                {
                    return new DynMethodResult(DateTime.Now);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "getdatetimeastext" },
                Body = (DynLanContext, Parameters) =>
                {
                    DateTime now = DateTime.Now.Date;
                    String nowText = StringHelper.FormatDate(now, "yyyymmddThhmiss");
                    return new DynMethodResult(nowText);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "getdate" },
                Body = (DynLanContext, Parameters) =>
                {
                    return new DynMethodResult(DateTime.Now.Date);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "getdateastext" },
                Body = (DynLanContext, Parameters) =>
                {
                    DateTime now = DateTime.Now.Date;
                    String nowText = StringHelper.FormatDate(now, "yyyymmdd");
                    return new DynMethodResult(nowText);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "return" },
                Body = (DynLanContext, Parameters) =>
                {
#if !NET20
                    return new DynMethodResult(Parameters.FirstOrDefault());
#else
                    return new DynMethodResult(Linq2.FirstOrDefault(Parameters));
#endif
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "substring" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        String str = UniConvert.ToUniString(Parameters[0] ?? "");
                        if (Parameters.Count > 2)
                        {
                            Int32 start = Convert.ToInt32(Parameters[1]);
                            Int32 len = Convert.ToInt32(Parameters[2]);
                            if (start < str.Length)
                            {
                                if (start + len <= str.Length)
                                {
                                    return new DynMethodResult(str.Substring(start, len));
                                }
                                else
                                {
                                    return new DynMethodResult(str.Substring(start));
                                }
                            }
                            return new DynMethodResult("");
                        }
                        else if (Parameters.Count == 2)
                        {
                            Int32 start = Convert.ToInt32(Parameters[1]);
                            if (start < str.Length)
                            {
                                return new DynMethodResult(str.Substring(start));
                            }
                            return new DynMethodResult("");
                        }
                        return new DynMethodResult(str);
                    }
                    return new DynMethodResult("");
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "split" },
                Body = (DynLanContext, Parameters) =>
                {
#if NETCE
                    throw new NotImplementedException();
#else
                    if (Parameters.Count > 0)
                    {
                        List <String> list = new List <String>();
                        String str = UniConvert.ToUniString(Parameters[0] ?? "");
                        if (Parameters.Count > 1)
                        {
                            List <String> splitParams = new List <String>();
                            for (Int32 i = 1; i < Parameters.Count; i++)
                            {
                                splitParams.Add(UniConvert.ToString(Parameters[i] ?? ""));
                            }

#if NET20
                            foreach (String item in str.Split(splitParams.ToArray(), StringSplitOptions.None))
#else
                            foreach (String item in str.Split(splitParams.ToArray(), StringSplitOptions.None))
#endif
                            { list.Add(item); }
                        }
                        else
                        {
                            list.Add(str);
                        }
                        return new DynMethodResult(list);
                    }
                    return new DynMethodResult("");
#endif
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "str" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        return new DynMethodResult(
                            UniConvert.ToString(Parameters[0]));
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "lower" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        if (Parameters[0] == null)
                        {
                            return null;
                        }
                        return new DynMethodResult(
                            UniConvert.ToString(Parameters[0]).ToLower());
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "upper" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        if (Parameters[0] == null)
                        {
                            return null;
                        }
                        return new DynMethodResult(
                            UniConvert.ToString(Parameters[0]).ToUpper());
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "ceil" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        return new DynMethodResult(
                            UniConvert.ToDecimal(Math.Ceiling(UniConvert.ToDouble(Parameters[0]))));
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "eval" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
#if !NET20
                        DynLanProgram program = new Compiler().Compile(
                            UniConvert.ToString(Parameters.First()));
#else
                        DynLanProgram program = new Compiler().Compile(
                            UniConvert.ToString(Linq2.FirstOrDefault(Parameters)));
#endif

                        EvaluatorForMethods.EvaluateMethod(
                            null,
                            program,
                            null,
                            DynLanContext);

                        return new DynMethodResult()
                        {
                            NewContextCreated = true
                        };
                    }
                    return new DynMethodResult(null);
                }
            });

            /*yield return new ExpressionMethod()
             * {
             *  OperationNames = new[] { "bindtoevent" },
             *  CalculateValueDelegate = (DynLanContext, Parameters) =>
             *  {
             *      if (Parameters.Count > 0)
             *      {
             *          Object obj = Parameters != null && Parameters.Count > 0 ? Parameters[0] : null;
             *          String eventName = ((Parameters != null && Parameters.Count > 1 ? Parameters[1] : null) ?? "").ToString().Trim();
             *          Object method = Parameters != null && Parameters.Count > 2 ? Parameters[2] : null;
             *
             *          throw new NotImplementedException();
             *
             *          MyReflectionHelper.BindToEvent(
             *              obj,
             *              eventName,
             *              (s, o) =>
             *              {
             *
             *              });
             *      }
             *      return new ExpressionMethodResult(null);
             *  }
             * };*/
            /*yield return new ExpressionMethod()
             * {
             *  OperationNames = new[] { "create" },
             *  CalculateValueDelegate = (DynLanContext, Parameters) =>
             *  {
             *      if (Parameters.Count <= 0)
             *          return new ExpressionMethodResult(null);
             *
             *      String typeName = UniConvert.ToString(Parameters[0]) ?? "";
             *
             *      foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
             *      {
             *          var type = assembly.GetType(typeName);
             *          if (type == null)
             *              continue;
             *
             *          if (type.IsStatic())
             *          {
             *              return null;
             *          }
             *          else
             *          {
             *              var r = Activator.CreateInstance(type);
             *              return new ExpressionMethodResult(r);
             *          }
             *      }
             *
             *      return new ExpressionMethodResult(null);
             *  }
             * };*/
            yield return(new DynMethod()
            {
                Names = new[] { "type" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count <= 0)
                    {
                        return new DynMethodResult(null);
                    }

                    String typeName = UniConvert.ToString(Parameters[0]) ?? "";
                    var type = MyAssemblyHelper.FindType(typeName);
                    return new DynMethodResult(type);
                }
            });

#if !PCL
            yield return(new DynMethod()
            {
                Names = new[] { "sleep" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        Int32 milliseconds = UniConvert.ToInt32(Parameters[0]);
                        Thread.Sleep(milliseconds);
                        return new DynMethodResult(milliseconds);
                    }
                    return new DynMethodResult(0);
                }
            });
#endif
            yield return(new DynMethod()
            {
                Names = new[] { "new" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count <= 0)
                    {
                        return new DynMethodResult(null);
                    }

                    String typeName = UniConvert.ToString(Parameters[0]) ?? "";
                    var obj = MyAssemblyHelper.CreateType(typeName);
                    return new DynMethodResult(obj);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "list" },
                Body = (DynLanContext, Parameters) =>
                {
                    return new DynMethodResult(new List <Object>());
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "dictionary" },
                Body = (DynLanContext, Parameters) =>
                {
                    return new DynMethodResult(new Dictionary <String, Object>());
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "typeof" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
#if !NET20
                        if (Parameters.First() != null)
                        {
                            return new DynMethodResult(
                                Parameters.First().GetType());
                        }
#else
                        if (Linq2.FirstOrDefault(Parameters) != null)
                        {
                            return new DynMethodResult(
                                Linq2.FirstOrDefault(Parameters).GetType());
                        }
#endif
                    }
                    return new DynMethodResult(null);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "istypeof" },
                Body = (DynLanContext, Parameters) =>
                {
                    Boolean result = false;
                    if (Parameters.Count > 1)
                    {
                        Object obj = Parameters[0];
                        Object type = Parameters[1];

                        if (obj != null && type != null)
                        {
                            if (type is Type)
                            {
                                result = MyTypeHelper.Is(obj.GetType(), (Type)type);
                            }
                            else
                            {
                                String typeName = UniConvert.ToString(type);
                                result = MyTypeHelper.Is(obj.GetType(), typeName);
                            }
                        }
                    }
                    return new DynMethodResult(result);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "isnottypeof" },
                Body = (DynLanContext, Parameters) =>
                {
                    Boolean result = true;
                    if (Parameters.Count > 1)
                    {
                        Object obj = Parameters[0];
                        Object type = Parameters[1];

                        if (obj != null && type != null)
                        {
                            if (type is Type)
                            {
                                result = !MyTypeHelper.Is(obj.GetType(), (Type)type);
                            }
                            else
                            {
                                String typeName = UniConvert.ToString(type);
                                result = !MyTypeHelper.Is(obj.GetType(), typeName);
                            }
                        }
                    }
                    return new DynMethodResult(result);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "mod" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 1)
                    {
                        return new DynMethodResult(
                            UniConvert.ToDecimal(
                                UniConvert.ToDecimal(Parameters[0]) % UniConvert.ToDecimal(Parameters[1])));
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "newguid" },
                Body = (DynLanContext, Parameters) =>
                {
                    return new DynMethodResult(Guid.NewGuid());
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "trim" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        object val = Parameters[0];
                        if (val == null)
                        {
                            return null;
                        }

                        return new DynMethodResult(
                            UniConvert.ToString(val).Trim());
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "float", "double", "tofloat", "todouble", "decimal", "todecimal" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        return new DynMethodResult(
                            UniConvert.ToDecimal(Parameters[0]));
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "isfloat", "isdouble", "isdecimal", "isnumeric" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        Object val = Parameters[0];
                        if (val != null)
                        {
                            if (MyTypeHelper.IsNumeric(val))
                            {
                                return new DynMethodResult(
                                    UniConvert.ToDecimal(true));
                            }
                            String strval = UniConvert.ToString(val);
                            Boolean result = false;

#if NETCE
                            try
                            {
                                Decimal.Parse(strval);
                                result = true;
                            }
                            catch { }
#else
                            Decimal numval = 0;
                            if (Decimal.TryParse(strval, out numval))
                            {
                                result = true;
                            }
#endif

                            return new DynMethodResult(
                                UniConvert.ToDecimal(result));
                        }
                    }
                    return new DynMethodResult(false);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "int", "toint" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        return new DynMethodResult(
                            UniConvert.ToInt64(Parameters[0]));
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "floor" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        return new DynMethodResult(
                            UniConvert.ToDecimal(
                                Math.Floor(
                                    UniConvert.ToDouble(Parameters[0]))));
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "round" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        if (Parameters.Count == 1)
                        {
                            return new DynMethodResult(
                                UniConvert.ToDecimal(
                                    Math.Round(
                                        UniConvert.ToDouble(Parameters[0]))));
                        }
                        else
                        {
                            return new DynMethodResult(
                                UniConvert.ToDecimal(
                                    Math.Round(
                                        UniConvert.ToDouble(Parameters[0]),
                                        Convert.ToInt32(Parameters[1]))));
                        }
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "abs" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        if (Parameters.Count == 1)
                        {
                            return new DynMethodResult(
                                UniConvert.ToDecimal(
                                    Math.Abs(
                                        UniConvert.ToDouble(Parameters[0]))));
                        }
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "coalesce" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        var value = Parameters[0];
                        if (value == null || value is Undefined)
                        {
                            for (var i = 1; i < Parameters.Count; i++)
                            {
                                var nextVal = Parameters[i];
                                if (nextVal != null && !(nextVal is Undefined))
                                {
                                    return new DynMethodResult(nextVal);
                                }
                            }
                        }
                        return new DynMethodResult(value);
                    }
                    return new DynMethodResult(null);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "not" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        Object val = (Parameters[0] ?? "");
                        if (val != null && !(val is Undefined))
                        {
                            Type type = val.GetType();
                            if (MyTypeHelper.IsNumeric(type))
                            {
                                Decimal numVal = UniConvert.ToDecimal(val);
                                return new DynMethodResult(Math.Sign(numVal) == 0 ? true : false);
                            }
                            else if (type.Name == "String")
                            {
                                return new DynMethodResult(
                                    String.IsNullOrEmpty(
                                        (String)val));
                            }
                            else
                            {
                                return new DynMethodResult(false);
                            }
                        }
                        else
                        {
                            return new DynMethodResult(true);
                        }
                    }
                    return new DynMethodResult(null);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "getyear" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        Object val = Parameters[0];
                        if (val != null && (val.GetType() == typeof(DateTime) || val.GetType() == typeof(DateTime?)))
                        {
                            return new DynMethodResult(((DateTime)val).Year);
                        }
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "len", "length" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        String val = Convert.ToString(Parameters[0] ?? "");
                        return new DynMethodResult(val.Length);
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "getmonth" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        Object val = Parameters[0];
                        if (val != null && (val.GetType() == typeof(DateTime) || val.GetType() == typeof(DateTime?)))
                        {
                            return new DynMethodResult(((DateTime)val).Month);
                        }
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "getday" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        Object val = Parameters[0];
                        if (val != null && (val.GetType() == typeof(DateTime) || val.GetType() == typeof(DateTime?)))
                        {
                            return new DynMethodResult(((DateTime)val).Day);
                        }
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "hours" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        Object val = Parameters[0];
                        if (val != null && (val.GetType() == typeof(TimeSpan) || val.GetType() == typeof(TimeSpan?)))
                        {
                            return new DynMethodResult(Convert.ToDecimal(((TimeSpan)val).TotalHours));
                        }
                        if (val != null && (val.GetType() == typeof(DateTime) || val.GetType() == typeof(DateTime?)))
                        {
                            return new DynMethodResult(Convert.ToDecimal(TimeSpan.FromTicks(((DateTime)val).Ticks).TotalHours));
                        }
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "days" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        Object val = Parameters[0];
                        if (val != null && (val.GetType() == typeof(TimeSpan) || val.GetType() == typeof(TimeSpan?)))
                        {
                            return new DynMethodResult(Convert.ToDecimal(((TimeSpan)val).TotalDays));
                        }
                        if (val != null && (val.GetType() == typeof(DateTime) || val.GetType() == typeof(DateTime?)))
                        {
                            return new DynMethodResult(Convert.ToDecimal(TimeSpan.FromTicks(((DateTime)val).Ticks).TotalDays));
                        }
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "minutes" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        Object val = Parameters[0];
                        if (val != null && (val.GetType() == typeof(TimeSpan) || val.GetType() == typeof(TimeSpan?)))
                        {
                            return new DynMethodResult(Convert.ToDecimal(((TimeSpan)val).TotalMinutes));
                        }
                        if (val != null && (val.GetType() == typeof(DateTime) || val.GetType() == typeof(DateTime?)))
                        {
                            return new DynMethodResult(Convert.ToDecimal(TimeSpan.FromTicks(((DateTime)val).Ticks).TotalMinutes));
                        }
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "seconds" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        Object val = Parameters[0];
                        if (val != null && (val.GetType() == typeof(TimeSpan) || val.GetType() == typeof(TimeSpan?)))
                        {
                            return new DynMethodResult(Convert.ToDecimal(((TimeSpan)val).TotalSeconds));
                        }
                        if (val != null && (val.GetType() == typeof(DateTime) || val.GetType() == typeof(DateTime?)))
                        {
                            return new DynMethodResult(Convert.ToDecimal(TimeSpan.FromTicks(((DateTime)val).Ticks).TotalSeconds));
                        }
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "milliseconds" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        Object val = Parameters[0];
                        if (val != null && (val.GetType() == typeof(TimeSpan) || val.GetType() == typeof(TimeSpan?)))
                        {
                            return new DynMethodResult(Convert.ToDecimal(((TimeSpan)val).TotalMilliseconds));
                        }
                        if (val != null && (val.GetType() == typeof(DateTime) || val.GetType() == typeof(DateTime?)))
                        {
                            return new DynMethodResult(Convert.ToDecimal(TimeSpan.FromTicks(((DateTime)val).Ticks).TotalMilliseconds));
                        }
                    }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "weekofyear" },
                Body = (DynLanContext, Parameters) =>
                {
                    Int32?val = null;
                    if (Parameters.Count > 0)
                    {
                        try
                        {
                            DateTime?val2 = UniConvert.ToDateTimeN(Parameters[0]);
                            if (val2 != null)
                            {
                                return new DynMethodResult(
                                    (Int32)Math.Floor(val2.Value.DayOfYear / 7.0) + 1);
                            }
                        }
                        catch { }
                    }
                    return new DynMethodResult(val);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "todatetime" },
                Body = (DynLanContext, Parameters) =>
                {
                    DateTime date = new DateTime();
                    if (Parameters.Count > 0)
                    {
                        Object val = Parameters[0];
                        String str = UniConvert.ToUniString(val ?? "");
                        if (UniConvert.TryParseUniDateTime(str, out date))
                        {
                        }
                    }
                    return new DynMethodResult(date);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "todate" },
                Body = (DynLanContext, Parameters) =>
                {
                    DateTime date = new DateTime();
                    if (Parameters.Count > 0)
                    {
                        Object val = Parameters[0];
                        String str = UniConvert.ToUniString(val ?? "");
                        if (UniConvert.TryParseUniDateTime(str, out date))
                        {
                        }
                    }
                    return new DynMethodResult(date.Date);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "format" },
                Body = (DynLanContext, Parameters) =>
                {
                    String r = "";
                    if (Parameters.Count > 1)
                    {
                        Object val = Parameters[0];
                        String format = Convert.ToString(Parameters[1] ?? "");
                        if (val != null && (val is DateTime || val is DateTime?))
                        {
                            r = StringHelper.FormatDate((DateTime)val, format);
                        }
                    }
                    return new DynMethodResult(r);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "tostring" },
                Body = (DynLanContext, Parameters) =>
                {
                    if (Parameters.Count > 0)
                    {
                        var result = UniConvert.ToUniString(Parameters[0] ?? "");
                        if (Parameters.Count > 1)
                        {
                            var len = Convert.ToInt32(Parameters[1]);
                            var txt = (Convert.ToString(Parameters[2]) ?? "");
                            txt = txt.Replace("\"", "").Replace("'", "");

                            while (result.Length < len)
                            {
                                result = txt + result;
                            }
                        }
                        return new DynMethodResult(result);
                    }
                    return new DynMethodResult("");
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "sqrt" },
                Body = (DynLanContext, Parameters) =>
                {
                    try
                    {
                        if (Parameters.Count > 0)
                        {
                            Object val = Parameters[0];
                            Decimal result = UniConvert.ToDecimal(val);
                            return new DynMethodResult((decimal)Math.Sqrt((double)result));
                        }
                    }
                    catch { }
                    return new DynMethodResult(0);
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "adddays" },
                Body = (DynLanContext, Parameters) =>
                {
                    try
                    {
                        if (Parameters.Count > 0)
                        {
                            Object v = Parameters[0];
                            if (v != null && (v.GetType() == typeof(DateTime) || v.GetType() == typeof(DateTime?)))
                            {
                                DateTime date = (DateTime)v;
                                if (Parameters.Count > 0)
                                {
                                    var days = UniConvert.ToInt32(Parameters[1]);
                                    date = date.AddDays(days);
                                }
                                return new DynMethodResult(date);
                            }
                        }
                        return new DynMethodResult(
                            new DateTime());
                    }
                    catch
                    {
                        return new DynMethodResult(0);
                    }
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "addyears" },
                Body = (DynLanContext, Parameters) =>
                {
                    try
                    {
                        if (Parameters.Count > 0)
                        {
                            Object v = Parameters[0];
                            if (v != null && (v.GetType() == typeof(DateTime) || v.GetType() == typeof(DateTime?)))
                            {
                                DateTime date = (DateTime)v;
                                if (Parameters.Count > 0)
                                {
                                    var years = UniConvert.ToInt32(Parameters[1]);
                                    date = date.AddYears(years);
                                }
                                return new DynMethodResult(date);
                            }
                        }
                        return new DynMethodResult(new DateTime());
                    }
                    catch
                    {
                        return new DynMethodResult(0);
                    }
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "addmonths" },
                Body = (DynLanContext, Parameters) =>
                {
                    try
                    {
                        if (Parameters.Count > 0)
                        {
                            Object v = Parameters[0];
                            if (v != null && (v.GetType() == typeof(DateTime) || v.GetType() == typeof(DateTime?)))
                            {
                                DateTime date = (DateTime)v;
                                if (Parameters.Count > 0)
                                {
                                    var months = UniConvert.ToInt32(Parameters[1]);
                                    date = date.AddMonths(months);
                                }
                                return new DynMethodResult(date);
                            }
                        }
                        return new DynMethodResult(new DateTime());
                    }
                    catch
                    {
                        return new DynMethodResult(0);
                    }
                }
            });

            yield return(new DynMethod()
            {
                Names = new[] { "rand" },
                Body = (DynLanContext, Parameters) =>
                {
                    return new DynMethodResult(
                        Convert.ToDecimal(
                            new Random(DateTime.Now.Millisecond).NextDouble()));
                }
            });
        }
Ejemplo n.º 15
0
        public Object Validate(Object Value)
        {
            if (Type == S4JFieldType.ANY)
            {
                return(Value);
            }

            if (IsRequired && Value == null)
            {
                throw new S4JNullParameterException("Parameter " + Name + " cannot be null");
            }

            if (Value != null && Type == S4JFieldType.BOOL)
            {
                if (!MyTypeHelper.IsBoolean(Value.GetType()))
                {
                    throw new S4JInvalidParameterTypeException("Parameter " + Name + " should be of type boolean");
                }
            }

            if (Value != null && Type == S4JFieldType.DATETIME)
            {
                if (!MyTypeHelper.IsDateTime(Value.GetType()))
                {
                    throw new S4JInvalidParameterTypeException("Parameter " + Name + " should be of type datetime");
                }
            }

            if (Value != null && Type == S4JFieldType.FLOAT)
            {
                if (!MyTypeHelper.IsNumeric(Value.GetType()))
                {
                    throw new S4JInvalidParameterTypeException("Parameter " + Name + " should be of type float");
                }
            }

            if (Value != null && Type == S4JFieldType.INT)
            {
                if (!MyTypeHelper.IsInteger(Value.GetType()))
                {
                    throw new S4JInvalidParameterTypeException("Parameter " + Name + " should be of type integer");
                }
            }

            if (Value != null && Type == S4JFieldType.STRING)
            {
                if (!MyTypeHelper.IsString(Value.GetType()))
                {
                    throw new S4JInvalidParameterTypeException("Parameter " + Name + " should be of type string");
                }
            }

            if (Type == S4JFieldType.ARRAY)
            {
                if (Value == null)
                {
                    return(new List <Object>());
                }
                else if (!(Value is IList))
                {
                    //if (MyTypeHelper.IsClass(Value.GetType()) || Value is IDictionary<String, Object>)
                    //    return new List<Object>() { Value };
                    throw new S4JInvalidParameterTypeException("Parameter " + Name + " should be of type array");
                }
            }

            if (Value != null && Type == S4JFieldType.OBJECT)
            {
                if (!(MyTypeHelper.IsClass(Value.GetType()) || Value is IDictionary <String, Object>) || Value is IList)
                {
                    throw new S4JInvalidParameterTypeException("Parameter " + Name + " should be of type object");
                }
            }

            return(Value);
        }
Ejemplo n.º 16
0
        ////////////////////////////////////////////////

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

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

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

            if (type1 == null || type2 == null)
            {
                return(null);
            }

            if (OperationType == OperatorType.EQUAL)
            {
                Boolean r = Value1.IsEqualWithNumericConvert(Value2);
                return(r);
            }
            else if (OperationType == OperatorType.NOT_EQUAL)
            {
                Boolean r = !Value1.IsEqualWithNumericConvert(Value2);
                return(r);
            }
            else if (OperationType == OperatorType.PROPERTY)
            {
                Object outValue = InternalTypeConverter.ToOuter(Value1).GetValue(UniConvert.ToString(Value2));
                return(InternalTypeConverter.ToInner(outValue));
            }

            /*else if (OperationType == OperatorType.METHODCALL)
             * {
             *  Object outValue = InternalTypeConverter.ToOuter(Value1).GetValue(UniConvert.ToString(Value2));
             *  return InternalTypeConverter.ToInner(outValue);
             * }*/
            else if (MyTypeHelper.IsNumeric(type1) && MyTypeHelper.IsNumeric(type2))
            {
                Decimal value1 = UniConvert.
                                 ToDecimal(Value1);

                Decimal value2 = UniConvert.
                                 ToDecimal(Value2);

                if (OperationType == OperatorType.PLUS)
                {
                    result = value1 + value2;
                }
                else if (OperationType == OperatorType.DIVIDE)
                {
                    result = value1 / value2;
                }
                else if (OperationType == OperatorType.MULTIPLY)
                {
                    result = value1 * value2;
                }
                else if (OperationType == OperatorType.SUBTRACT)
                {
                    result = value1 - value2;
                }
                else if (OperationType == OperatorType.GREATER)
                {
                    result = value1 > value2;
                }
                else if (OperationType == OperatorType.SMALLER)
                {
                    result = value1 < value2;
                }
                else if (OperationType == OperatorType.GREATER_OR_EQUAL)
                {
                    result = value1 >= value2;
                }
                else if (OperationType == OperatorType.SMALLER_OR_EQUAL)
                {
                    result = value1 <= value2;
                }
                else if (OperationType == OperatorType.EQUAL)
                {
                    result = value1 == value2;
                }
                else if (OperationType == OperatorType.NOT_EQUAL)
                {
                    result = value1 != value2;
                }
                else if (OperationType == OperatorType.AND)
                {
                    result = (value1 != 0) && (value2 != 0);
                }
                else if (OperationType == OperatorType.OR)
                {
                    result = (value1 != 0) || (value2 != 0);
                }
                else
                {
                    throw new PainInvalidOperationException();
                }
            }
            else
            {
                if (type1 == typeof(TimeSpan) || type2 == typeof(TimeSpan))
                {
                    throw new PainInvalidOperationException("Invalid operation type " + OperationType + " (TimeSpan & TimeSpan)");
                }
                else if (OperationType == OperatorType.MULTIPLY && type1.IsNumeric() && 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 && type2.IsNumeric() && 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 PainInvalidOperationException();
                    }
                }
                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 PainInvalidOperationException("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 PainInvalidOperationException("Invalid operation type " + OperationType + " (DateTime & numeric)");
                    }
                }
                else
                {
                    throw new PainInvalidOperationException("Invalid operation type " + OperationType + " (" + (type1 == null ? "null" : type1.Name) + " & " + (type2 == null ? "null" : type2.Name) + ")");
                }
            }

            return(result);
        }