zoukankan      html  css  js  c++  java
  • 一个转换帮助类

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

      

  • 相关阅读:
    堆栈、堆、方法区介绍
    spring 定时器
    fastJSON 使用总结
    [Python]collections.defaultdict()模块使用
    LeetCode 18.四数之和
    [Python]调用shell cmd的几种方式
    LeetCode 16. 最接近的三数之和
    Objective C 十六进制 十进制互转
    LeetCode 15. 三数之和
    要做的题
  • 原文地址:https://www.cnblogs.com/lpfsky/p/3893731.html
Copyright © 2011-2022 走看看