Esempio n. 1
0
        /// <summary>
        /// returns the converted object or defaultValue if not convertable, object is null or DBnull
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static DateTime?ToDateTime(object value, DateTime?defaultValue)
        {
            if (value is DateTime)
            {
                return((DateTime)value);
            }

            try
            {
                if (!DeVesValidator.IsNullState(value))
                {
                    var _s = value as string;
                    if (_s != null)
                    {
                        DateTime _val;
                        if (DateTime.TryParse(_s, out _val))
                        {
                            return(_val);
                        }
                    }
                    else
                    {
                        return(Convert.ToDateTime(value));
                    }
                }
            }
            catch (Exception)
            {
                // ignored
            }
            return(defaultValue);
        }
Esempio n. 2
0
        /// <summary>
        /// returns the converted object or defaultValue if not convertable, object is null, Empty or DBnull. Preceding and trailing white spaces are removed.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <param name="enumAsInteger"></param>
        /// <returns></returns>
        public static string ToString(object value, string defaultValue = null, bool enumAsInteger = true)
        {
            try
            {
                if (!DeVesValidator.IsNullState(value))
                {
                    if (value.GetType().BaseType == typeof(Enum) && enumAsInteger)
                    {
                        return(((int)value).ToString());
                    }

                    var _value = value.ToString();
                    if (!DeVesValidator.IsNullState(_value))
                    {
                        _value = _value.Trim();
                        return((_value == string.Empty) ? defaultValue : _value);
                    }
                }
            }
            catch (Exception)
            {
                // ignored
            }

            return(defaultValue);
        }
Esempio n. 3
0
        /// <summary>
        /// returns the converted object or errorValue if not convertable, defaultValue if the object is null or DBnull
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue">value to be returned in case of Null-Value</param>
        /// <param name="errorValue">value to be return in case that the vonversion fails</param>
        /// <returns>the converted, default or error value</returns>
        public static double?ToDouble(object value, double?defaultValue, double?errorValue)
        {
            if (value is double)
            {
                return((double)value);
            }

            try
            {
                if (!DeVesValidator.IsNullState(value))
                {
                    var _s = value as string;
                    if (_s != null)
                    {
                        double _val;
                        if (double.TryParse(_s, out _val))
                        {
                            return(_val);
                        }
                    }
                    else
                    {
                        return(Convert.ToDouble(value));
                    }
                }
            }
            catch (Exception)
            {
                return(errorValue);
            }
            return(defaultValue);
        }
Esempio n. 4
0
        /// <summary>
        /// returns the converted object or defaultValue if not convertable, object is null or DBnull
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool?ToBool(object value, bool?defaultValue)
        {
            if (value is bool)
            {
                return((bool)value);
            }
            try
            {
                if (!DeVesValidator.IsNullState(value))
                {
                    switch (value.ToString().ToLower())
                    {
                    case "true":
                        return(true);

                    case "false":
                        return(false);

                    default:
                        return(Convert.ToInt32(value) != 0);
                    }
                }
            }
            catch (Exception)
            {
                // ignored
            }
            return(defaultValue);
        }
Esempio n. 5
0
 /// <summary>
 /// returns the converted object or errorValue if not convertable, defaultValue if the object is null or DBnull
 /// </summary>
 /// <param name="value"></param>
 /// <param name="defaultValue">value to be returned in case of Null-Value</param>
 /// <param name="errorValue">value to be return in case that the vonversion fails</param>
 /// <returns>the converted, default or error value</returns>
 public static int?ToInteger(object value, int?defaultValue, int?errorValue)
 {
     if (value is int)
     {
         return((int)value);
     }
     try
     {
         if (!DeVesValidator.IsNullState(value))
         {
             var _s = value as string;
             if (_s != null)
             {
                 int _val;
                 if (int.TryParse(_s, out _val))
                 {
                     return(_val);
                 }
             }
             else
             {
                 return(Convert.ToInt32(value));
             }
         }
     }
     catch (Exception)
     {
         return(errorValue);
     }
     return(defaultValue);
 }
Esempio n. 6
0
 /// <summary>
 /// returns the converted object or errorValue if not convertable, defaultValue if the object is null or DBnull
 /// </summary>
 /// <param name="value"></param>
 /// <param name="defaultValue">value to be returned in case of Null-Value</param>
 /// <param name="errorValue">value to be return in case that the vonversion fails</param>
 /// <returns>the converted, default or error value</returns>
 public static long?ToLong(object value, long?defaultValue, long?errorValue)
 {
     if (value is long)
     {
         return((long)value);
     }
     try
     {
         if (!DeVesValidator.IsNullState(value))
         {
             var _s = value as string;
             if (_s != null)
             {
                 long _val;
                 if (long.TryParse(_s, out _val))
                 {
                     return(_val);
                 }
             }
             else
             {
                 return(Convert.ToInt64(value));
             }
         }
     }
     catch (Exception)
     {
         return(errorValue);
     }
     return(defaultValue);
 }
Esempio n. 7
0
 /// <summary>
 /// returns the converted object or errorValue if not convertable, defaultValue if the object is null or DBnull
 /// </summary>
 /// <param name="value"></param>
 /// <param name="defaultValue">value to be returned in case of Null-Value</param>
 /// <param name="errorValue">value to be return in case that the vonversion fails</param>
 /// <returns>the converted, default or error value</returns>
 public static short?ToShort(object value, short?defaultValue, short?errorValue)
 {
     if (value is short)
     {
         return((short)value);
     }
     try
     {
         if (!DeVesValidator.IsNullState(value))
         {
             var _s = value as string;
             if (_s != null)
             {
                 short _val;
                 if (short.TryParse(_s, out _val))
                 {
                     return(_val);
                 }
             }
             else
             {
                 return(Convert.ToInt16(value));
             }
         }
     }
     catch (Exception)
     {
         return(errorValue);
     }
     return(defaultValue);
 }
Esempio n. 8
0
        public static bool Is(Type valueType, params Type[] findTypes)
        {
            if (findTypes == null)
            {
                return(false);
            }
            if (!findTypes.Any())
            {
                return(false);
            }

            if (findTypes.Contains(valueType))
            {
                return(true);
            }

            return(!DeVesValidator.IsNullState(valueType.BaseType) && DeVesValidator.Is(valueType.BaseType, findTypes));
        }
Esempio n. 9
0
        public static IEnumerable <string> LoadSqlServerInstances(bool onlyLocal = true)
        {
            if (onlyLocal)
            {
                return(LoadLocalSqlServerInstances());
            }

            var _table = SqlDataSourceEnumerator.Instance.GetDataSources();

            var _rows = _table.Rows.Cast <DataRow>().ToArray();

            _rows = _rows.Where(row => !DeVesValidator.IsNullState(row["ServerName"]) && !DeVesValidator.IsNullState(row["InstanceName"])).ToArray();

            var _instances = _rows.Select(row => string.Format("{0}\\{1}", row["ServerName"], row["InstanceName"]));

            DeVesHelper.DisposeDataTable(ref _table);

            return(_instances);
        }
Esempio n. 10
0
        public static TValue To <TValue>(object value, TValue defaultValue)
        {
            if (DeVesValidator.Is <TValue>(typeof(string)))
            {
                return((TValue)(object)DeVesConverter.ToString(value, (string)(object)defaultValue));
            }

            if (DeVesValidator.Is <TValue>(typeof(short), typeof(short?)))
            {
                return((TValue)(object)DeVesConverter.ToShort(value, (short?)(object)defaultValue));
            }
            if (DeVesValidator.Is <TValue>(typeof(int), typeof(int?)))
            {
                return((TValue)(object)DeVesConverter.ToInteger(value, (int?)(object)defaultValue));
            }
            if (DeVesValidator.Is <TValue>(typeof(long), typeof(long?)))
            {
                return((TValue)(object)DeVesConverter.ToLong(value, (long?)(object)defaultValue));
            }

            if (DeVesValidator.Is <TValue>(typeof(double), typeof(double?)))
            {
                return((TValue)(object)DeVesConverter.ToDouble(value, (double?)(object)defaultValue));
            }

            if (DeVesValidator.Is <TValue>(typeof(bool), typeof(bool?)))
            {
                return((TValue)(object)DeVesConverter.ToBool(value, (bool?)(object)defaultValue));
            }

            if (DeVesValidator.Is <TValue>(typeof(DateTime), typeof(DateTime?)))
            {
                return((TValue)(object)DeVesConverter.ToDateTime(value, (DateTime?)(object)defaultValue));
            }

            return(defaultValue);
        }
Esempio n. 11
0
        //public static bool Is(Type inheritedType, Type baseType)
        //{
        //    if (inheritedType.BaseType != null && inheritedType.BaseType == baseType)
        //        return true;

        //    if (inheritedType.BaseType != null)
        //        return DeVesValidator.Is(inheritedType.BaseType, baseType);

        //    return inheritedType == baseType;
        //}

        public static bool Is <TValue>(params Type[] findTypes)
        {
            return(DeVesValidator.Is(typeof(TValue), findTypes));
        }