123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- using System;
- namespace Utilities
- {
- public partial class Conversion
- {
- public static short ToInt16(object obj)
- {
- return ToInt16(obj, 0);
- }
- public static short ToInt16(object obj, short defaultValue)
- {
- short result = defaultValue;
- if (obj != null)
- {
- try
- {
- result = Convert.ToInt16(obj);
- }
- catch
- { }
- }
- return result;
- }
- public static int ToInt32(object obj)
- {
- return ToInt32(obj, 0);
- }
- public static int ToInt32(object obj, int defaultValue)
- {
- int result = defaultValue;
- if (obj != null)
- {
- try
- {
- result = Convert.ToInt32(obj);
- }
- catch
- {}
- }
- return result;
- }
- public static long ToInt64(object obj)
- {
- return ToInt64(obj, 0);
- }
- public static long ToInt64(object obj, long defaultValue)
- {
- long result = defaultValue;
- if (obj != null)
- {
- try
- {
- result = Convert.ToInt64(obj);
- }
- catch
- { }
- }
- return result;
- }
- public static ushort ToUInt16(object obj)
- {
- return ToUInt16(obj, 0);
- }
- public static ushort ToUInt16(object obj, ushort defaultValue)
- {
- ushort result = defaultValue;
- if (obj != null)
- {
- try
- {
- result = Convert.ToUInt16(obj);
- }
- catch
- { }
- }
- return result;
- }
- public static uint ToUInt32(object obj)
- {
- return ToUInt32(obj, 0);
- }
- public static uint ToUInt32(object obj, uint defaultValue)
- {
- uint result = defaultValue;
- if (obj != null)
- {
- try
- {
- result = Convert.ToUInt32(obj);
- }
- catch
- { }
- }
- return result;
- }
- public static ulong ToUInt64(object obj)
- {
- return ToUInt64(obj, 0);
- }
- public static ulong ToUInt64(object obj, ulong defaultValue)
- {
- ulong result = defaultValue;
- if (obj != null)
- {
- try
- {
- result = Convert.ToUInt64(obj);
- }
- catch
- { }
- }
- return result;
- }
- public static float ToFloat(object obj)
- {
- return ToFloat(obj, 0);
- }
- public static float ToFloat(object obj, float defaultValue)
- {
- float result = defaultValue;
- if (obj != null)
- {
- try
- {
- result = Convert.ToSingle(obj);
- }
- catch
- {}
- }
- return result;
- }
- public static double ToDouble(object obj)
- {
- return ToDouble(obj, 0);
- }
- public static double ToDouble(object obj, double defaultValue)
- {
- double result = defaultValue;
- if (obj != null)
- {
- try
- {
- result = Convert.ToDouble(obj);
- }
- catch
- { }
- }
- return result;
- }
- public static decimal ToDecimal(object obj)
- {
- return ToDecimal(obj, 0);
- }
- public static decimal ToDecimal(object obj, decimal defaultValue)
- {
- decimal result = defaultValue;
- if (obj != null)
- {
- try
- {
- result = Convert.ToDecimal(obj);
- }
- catch
- { }
- }
- return result;
- }
- public static bool ToBoolean(object obj)
- {
- return ToBoolean(obj, false);
- }
- public static bool ToBoolean(object obj, bool defaultValue)
- {
- bool result = defaultValue;
- if (obj != null)
- {
- try
- {
- result = Convert.ToBoolean(obj);
- }
- catch
- { }
- }
- return result;
- }
- public static string ToString(object obj)
- {
- string result = String.Empty;
- if (obj != null)
- {
- try
- {
- result = Convert.ToString(obj);
- }
- catch
- {}
- }
- return result;
- }
- public static char ToChar(object obj)
- {
- return ToChar(obj, new char());
- }
- public static char ToChar(object obj, char defaultValue)
- {
- char result = defaultValue;
- if (obj != null)
- {
- try
- {
- result = Convert.ToChar(obj);
- }
- catch
- { }
- }
- return result;
- }
- public static DateTime ToDateTime(object obj)
- {
- return ToDateTime(obj, DateTime.MinValue);
- }
- public static DateTime ToDateTime(object obj, DateTime defaultValue)
- {
- DateTime result = defaultValue;
- if (obj != null)
- {
- try
- {
- result = Convert.ToDateTime(obj);
- }
- catch
- { }
- }
- return result;
- }
- }
- }
|