コード例 #1
0
        void SetDefaultValue(object value, bool forcedTypeCheck)
        {
            if (forcedTypeCheck || !this._defaultValue.Equals(value))
            {
                if (value == null || value == DBNull.Value)
                {
                    _defaultValue = GetDefaultValueForType(DataType);
                }
                else if (DataType.IsInstanceOfType(value))
                {
                    _defaultValue = value;
                }
                else
                {
                    try {
                        _defaultValue = ConvertExtensions.ChangeType(value, DataType);
                    } catch (InvalidCastException) {
                        string msg = String.Format("Default Value of type '{0}' is not compatible with column type '{1}'", value.GetType(), DataType);
                        throw new DataException(msg);
                    }
                }
            }

            // store default value in the table if already belongs to
            if (Table != null && Table.DefaultValuesRowIndex != -1)
            {
                DataContainer [Table.DefaultValuesRowIndex] = _defaultValue;
            }
        }
コード例 #2
0
        //(note: o1 and o2 must both be of type Int32/Int64/Decimal/Double)
        internal static TypeCode ToSameType(ref IConvertible o1, ref IConvertible o2)
        {
            TypeCode tc1 = o1.GetTypeCode();
            TypeCode tc2 = o2.GetTypeCode();

            if (tc1 == tc2)
            {
                return(tc1);
            }

            if (tc1 == TypeCode.DBNull || tc2 == TypeCode.DBNull)
            {
                return(TypeCode.DBNull);
            }


            // is it ok to make such assumptions about the order of an enum?
            if (tc1 < tc2)
            {
                o1 = (IConvertible)ConvertExtensions.ChangeType(o1, tc2);
                return(tc2);
            }
            else
            {
                o2 = (IConvertible)ConvertExtensions.ChangeType(o2, tc1);
                return(tc1);
            }
        }
コード例 #3
0
        private IConvertible CalcStatisticalFunction(object[] values)
        {
            if (count < 2)
            {
                return(DBNull.Value);
            }

            double average = (double)ConvertExtensions.ChangeType(result, TypeCode.Double) / count;
            double res     = 0.0;

            foreach (object val in values)
            {
                if (val == null)
                {
                    continue;
                }

                double diff = average - (double)ConvertExtensions.ChangeType(val, TypeCode.Double);
                res += System.Math.Pow(diff, 2);
            }
            res /= (count - 1);

            if (function == AggregationFunction.StDev)
            {
                res = System.Math.Sqrt(res);
            }

            return(res);
        }
コード例 #4
0
        internal static int Compare(IComparable o1, IComparable o2, bool caseSensitive)
        {
            //TODO: turn this "conversion pipeline" into something nicer

            try {
                if (o1 is string && Numeric.IsNumeric(o2))
                {
                    o1 = (IComparable)ConvertExtensions.ChangeType(o1, o2.GetType());
                }
                else if (o2 is string && Numeric.IsNumeric(o1))
                {
                    o2 = (IComparable)ConvertExtensions.ChangeType(o2, o1.GetType());
                }
                else if (o1 is string && o2 is Guid)
                {
                    o2 = o2.ToString();
                }
                else if (o2 is string && o1 is Guid)
                {
                    o1 = o1.ToString();
                }
            } catch (FormatException) {
                throw new EvaluateException(String.Format("Cannot perform compare operation on {0} and {1}.", o1.GetType(), o2.GetType()));
            }

            if (o1 is string && o2 is string)
            {
                o1 = ((string)o1).TrimEnd(IgnoredTrailingChars);
                o2 = ((string)o2).TrimEnd(IgnoredTrailingChars);
                if (!caseSensitive)
                {
                    o1 = ((string)o1).ToLower();
                    o2 = ((string)o2).ToLower();
                }
            }

            if (o1 is DateTime && o2 is string && Thread.CurrentThread.CurrentCulture != CultureInfo.InvariantCulture)
            {
                // DateTime is always CultureInfo.InvariantCulture
                o2 = (IComparable)DateTime.Parse((string)o2, CultureInfo.InvariantCulture);
            }
            else if (o2 is DateTime && o1 is string &&
                     Thread.CurrentThread.CurrentCulture != CultureInfo.InvariantCulture)
            {
                // DateTime is always CultureInfo.InvariantCulture
                o1 = (IComparable)DateTime.Parse((string)o1, CultureInfo.InvariantCulture);
            }
            else if (o2 is DateTime && o1 is string && Thread.CurrentThread.CurrentCulture != CultureInfo.InvariantCulture)
            {
                // DateTime is always CultureInfo.InvariantCulture
                o1 = (IComparable)DateTime.Parse((string)o1, CultureInfo.InvariantCulture);
            }

            if (o1.GetType() != o2.GetType())
            {
                o2 = (IComparable)ConvertExtensions.ChangeType(o2, o1.GetType());
            }

            return(o1.CompareTo(o2));
        }
コード例 #5
0
        private TResult GetParameterValueOrDefault <TResult>(CommandParameter parameter, TResult defaultValue = default(TResult))
        {
            if (parameter == null)
            {
                return(defaultValue);
            }

            var value = parameter.GetEvaluatedValue(this.ToDictionary());

            if (value == null)
            {
                return(defaultValue);
            }

            if (string.IsNullOrEmpty(value.ToStringOrEmpty()))
            {
                return(defaultValue);
            }

            if (value is IConvertible)
            {
                return((TResult)ConvertExtensions.ChangeType(value, typeof(TResult)));
            }

            return((TResult)value);
        }
コード例 #6
0
        protected override int DoCompareValues(int index1, int index2)
        {
            object obj1 = _values [index1];
            object obj2 = _values [index2];

            if (obj1 == obj2)
            {
                return(0);
            }

            if (obj1 is IComparable)
            {
                try {
                    return(((IComparable)obj1).CompareTo(obj2));
                } catch {
                    if (obj2 is IComparable)
                    {
                        obj2 = ConvertExtensions.ChangeType(obj2, Type.GetTypeCode(obj1.GetType()));
                        return(((IComparable)obj1).CompareTo(obj2));
                    }
                }
            }

            return(String.Compare(obj1.ToString(), obj2.ToString()));
        }
コード例 #7
0
        public static T AskInput <T>(string message)
        {
            Console.WriteLine();
            Console.WriteLine(message);

            string input = Console.ReadLine();

            return(ConvertExtensions.ChangeType <T>(input));
        }
コード例 #8
0
        //extends to Int32/Int64/Decimal/Double
        internal static IConvertible Unify(IConvertible o)
        {
            switch (o.GetTypeCode())
            {
            case TypeCode.Char:
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
                return((IConvertible)ConvertExtensions.ChangeType(o, TypeCode.Int32));

            case TypeCode.UInt32:
                return((IConvertible)ConvertExtensions.ChangeType(o, TypeCode.Int64));

            case TypeCode.UInt64:
                return((IConvertible)ConvertExtensions.ChangeType(o, TypeCode.Decimal));

            case TypeCode.Single:
                return((IConvertible)ConvertExtensions.ChangeType(o, TypeCode.Double));

            default:
                return(o);
            }
        }
コード例 #9
0
        override public object Eval(DataRow row)
        {
            object val = expr.Eval(row);

            // TMPFIX :Eval shud never really return a null.. DBNull.Value but not null
            // needs to be done for all expressions .. for now ,just check for null
            if (val == null)
            {
                return(DBNull.Value);
            }

            if (val == DBNull.Value || val.GetType() == targetType)
            {
                return(val);
            }

            //--> String is always allowed
            if (targetType == typeof(string))
            {
                return(val.ToString());
            }

            //only TimeSpan <--> String is allowed
            if (targetType == typeof(TimeSpan))
            {
                if (val is string)
                {
                    return(TimeSpan.Parse((string)val));
                }
                else
                {
                    ThrowInvalidCastException(val);
                }
            }

            if (val is TimeSpan)
            {
                ThrowInvalidCastException(val);
            }

            //only Char <--> String/Int32/UInt32 is allowed
            if (val is Char && !(targetType == typeof(Int32) || targetType == typeof(UInt32)))
            {
                ThrowInvalidCastException(val);
            }

            if (targetType == typeof(Char) && !(val is Int32 || val is UInt32))
            {
                ThrowInvalidCastException(val);
            }

            //bool <--> Char/Single/Double/Decimal/TimeSpan/DateTime is not allowed
            if (val is Boolean && (targetType == typeof(Single) || targetType == typeof(Double) || targetType == typeof(Decimal)))
            {
                ThrowInvalidCastException(val);
            }

            if (targetType == typeof(Boolean) && (val is Single || val is Double || val is Decimal))
            {
                ThrowInvalidCastException(val);
            }

            //Convert throws the remaining invalid casts
            return(ConvertExtensions.ChangeType(val, targetType));
        }