コード例 #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);
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #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 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);
 }
コード例 #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 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);
 }
コード例 #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);
 }
コード例 #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));
        }
コード例 #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);
        }