|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658 |
- 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
-
- }
- }
|