|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace CnasSynchronousCommon
- {
- public class ConvertUtilType
- {
- #region ToInt32
-
- public static int? ToInt32(string obj)
- {
- return ToInt32(obj, null);
- }
-
- public static int? ToInt32(object obj)
- {
- return ToInt32(obj, null);
- }
-
- public static int? ToInt32(object obj, int? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToInt32(Convert.ToString(obj), defaultValue);
- }
-
- public static int? ToInt32(string obj, int? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- int outValue;
-
- if (int.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- #region ToInt16
-
- public static short? ToInt16(string obj)
- {
- return ToInt16(obj, null);
- }
-
- public static short? ToInt16(object obj)
- {
- return ToInt16(obj, null);
- }
-
- public static short? ToInt16(object obj, short? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToInt16(Convert.ToString(obj), defaultValue);
- }
-
- public static short? ToInt16(string obj, short? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- short outValue;
-
- if (short.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- #region ToInt64
-
- public static long? ToInt64(string obj)
- {
- return ToInt64(obj, null);
- }
-
- public static long? ToInt64(object obj)
- {
- return ToInt64(obj, null);
- }
-
- public static long? ToInt64(object obj, long? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToInt64(Convert.ToString(obj), defaultValue);
- }
-
- public static long? ToInt64(string obj, long? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- long outValue;
-
- if (long.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- #region ToUInt32
-
- public static uint? ToUInt32(string obj)
- {
- return ToUInt32(obj, null);
- }
-
- public static uint? ToUInt32(object obj)
- {
- return ToUInt32(obj, null);
- }
-
- public static uint? ToUInt32(object obj, uint? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToUInt32(Convert.ToString(obj), defaultValue);
- }
-
- public static uint? ToUInt32(string obj, uint? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- uint outValue;
-
- if (uint.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- #region ToUInt16
-
- public static ushort? ToUInt16(string obj)
- {
- return ToUInt16(obj, null);
- }
-
- public static ushort? ToUInt16(object obj)
- {
- return ToUInt16(obj, null);
- }
-
- public static ushort? ToUInt16(object obj, ushort? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToUInt16(Convert.ToString(obj), defaultValue);
- }
-
- public static ushort? ToUInt16(string obj, ushort? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- ushort outValue;
-
- if (ushort.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- #region ToUInt64
-
- public static ulong? ToUInt64(string obj)
- {
- return ToUInt64(obj, null);
- }
-
- public static ulong? ToUInt64(object obj)
- {
- return ToUInt64(obj, null);
- }
-
- public static ulong? ToUInt64(object obj, ulong? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToUInt64(Convert.ToString(obj), defaultValue);
- }
-
- public static ulong? ToUInt64(string obj, ulong? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- ulong outValue;
-
- if (ulong.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- #region ToDateTime
-
- public static DateTime? ToDateTime(string obj)
- {
- return ToDateTime(obj, null);
- }
-
- public static DateTime? ToDateTime(object obj)
- {
- return ToDateTime(obj, null);
- }
-
- public static DateTime? ToDateTime(object obj, DateTime? defaultValue)
- {
- if (obj == DBNull.Value || obj == null || obj.Equals(""))
- {
- return defaultValue;
- }
- return Convert.ToDateTime(Convert.ToDateTime(obj).ToString("yyyy-MM-dd HH:mm"));
- //return ToDateTime(Convert.ToString(obj), defaultValue);
- }
-
- public static DateTime? ToDateTime(string obj, DateTime? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- DateTime outValue;
-
- if (DateTime.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- #region ToBoolean
-
- public static bool? ToBoolean(string obj)
- {
- return ToBoolean(obj, null);
- }
-
- public static bool? ToBoolean(object obj)
- {
- return ToBoolean(obj, null);
- }
-
- public static bool? ToBoolean(object obj, bool? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToBoolean(Convert.ToString(obj), defaultValue);
- }
-
- public static bool? ToBoolean(string obj, bool? defaultValue)
- {
- if (!string.IsNullOrEmpty(obj))
- {
- bool flag;
- if (bool.TryParse(obj, out flag))
- {
- return new bool?(flag);
- }
- if (obj == "1")
- {
- return true;
- }
- if (obj == "0")
- {
- return false;
- }
- }
- return defaultValue;
-
- }
-
- #endregion
-
- #region ToString
-
- public static string ToString(object obj)
- {
- return ToString(obj, null);
- }
-
- public static string ToString(object obj, string defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return System.Convert.ToString(obj);
- }
-
- public static string ToDateString(DateTime? obj, string defaultValue)
- {
- if (obj == null)
- {
- return defaultValue;
- }
-
- return obj.Value.ToString("yyyy-MM-dd");
- }
-
- public static string ToDateString(object obj, string defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- DateTime? dateTime = ToDateTime(obj);
-
- return ToDateString(dateTime, defaultValue);
- }
-
-
- #endregion
-
- #region ToByte
-
- public static byte? ToByte(string obj)
- {
- return ToByte(obj, null);
- }
-
- public static byte? ToByte(object obj)
- {
- return ToByte(obj, null);
- }
-
- public static byte? ToByte(object obj, byte? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToByte(Convert.ToString(obj), defaultValue);
- }
-
- public static byte? ToByte(string obj, byte? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- byte outValue;
-
- if (byte.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- #region ToSByte
-
- public static sbyte? ToSByte(string obj)
- {
- return ToSByte(obj, null);
- }
-
- public static sbyte? ToSByte(object obj)
- {
- return ToSByte(obj, null);
- }
-
- public static sbyte? ToSByte(object obj, sbyte? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToSByte(Convert.ToString(obj), defaultValue);
- }
-
- public static sbyte? ToSByte(string obj, sbyte? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- sbyte outValue;
-
- if (sbyte.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- #region ToDouble
-
- public static double? ToDouble(string obj)
- {
- return ToDouble(obj, null);
- }
-
- public static double? ToDouble(object obj)
- {
- return ToDouble(obj, null);
- }
-
- public static double? ToDouble(object obj, double? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToDouble(Convert.ToString(obj), defaultValue);
- }
-
- public static double? ToDouble(string obj, double? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- double outValue;
-
- if (double.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- #region ToSingle
-
- public static Single? ToSingle(string obj)
- {
- return ToSingle(obj, null);
- }
-
- public static Single? ToSingle(object obj)
- {
- return ToSingle(obj, null);
- }
-
- public static Single? ToSingle(object obj, Single? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToSingle(Convert.ToString(obj), defaultValue);
- }
-
- public static Single? ToSingle(string obj, Single? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- Single outValue;
-
- if (Single.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- #region ToChar
-
- public static char? ToChar(string obj)
- {
- return ToChar(obj, null);
- }
-
- public static char? ToChar(object obj)
- {
- return ToChar(obj, null);
- }
-
- public static char? ToChar(object obj, char? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToChar(Convert.ToString(obj), defaultValue);
- }
-
- public static char? ToChar(string obj, char? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- char outValue;
-
- if (char.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- #region ToDecimal
-
- public static decimal? ToDecimal(string obj)
- {
- return ToDecimal(obj, null);
- }
-
- public static decimal? ToDecimal(object obj)
- {
- return ToDecimal(obj, null);
- }
-
- public static decimal? ToDecimal(object obj, decimal? defaultValue)
- {
- if (obj == DBNull.Value || obj == null)
- {
- return defaultValue;
- }
-
- return ToDecimal(Convert.ToString(obj), defaultValue);
- }
-
- public static decimal? ToDecimal(string obj, decimal? defaultValue)
- {
- if (string.IsNullOrEmpty(obj))
- {
- return defaultValue;
- }
-
- decimal outValue;
-
- if (decimal.TryParse(obj, out outValue))
- {
- return outValue;
- }
- else
- {
- return defaultValue;
- }
- }
-
- #endregion
-
- }
- }
|