Richard__Lee

导航

一个转换帮助类

        public static bool IsValidGuid(string guid)
        {
            try
            {
                Guid.Parse(guid);
                return true;
            }
            catch
            {
                return false;
            }
        
        }

        public static Guid ToGuid(object obj)
        {
            try
            {
                return Guid.Parse(obj.ToString());
            }
            catch
            {
                return Guid.NewGuid();
            }
        }
        public static int ToInt(object obj,int defaultValue=0)
        {
            try
            {
                return   Convert.ToInt32(obj);
            }
            catch
            {
                return defaultValue;
            }
            
        }
        public static bool ToBool(object obj)
        {
            try
            {
                return Convert.ToBoolean(obj);
            }
            catch
            {
                return false;
            }

        }

        public static DateTime ToDateTime(object obj)
        {
            try
            {
                return Convert.ToDateTime(obj);
            }
            catch
            {
                return DateTime.Now.GetPacificTime();
            }

        }
        public static string ToString(object obj)
        {
            try
            {
                string temp= obj.ToString().TrimEnd().TrimStart() ;
              //  return HttpUtility.UrlDecode(temp);
                return temp;
            }
            catch
            {
                return string.Empty;
            }

        }
        public static byte ToByte(object obj)
        {
            try
            {
                return Convert.ToByte(obj);
            }
            catch
            {
                return byte.MinValue;
            }

        }
        public static decimal ToDecimal(object obj)
        {
            try
            {
                return Convert.ToDecimal(obj);
            }
            catch
            {
                return 0;
            }

        }

        public static double ToDouble(object obj)
        {
            try
            {
                return Convert.ToDouble(obj);
            }
            catch
            {
                return 0;
            }

        }
    }
  

  

posted on 2014-08-06 09:35  Richard__Lee  阅读(161)  评论(0编辑  收藏  举报