• .net扩展方法——类型转换


    .net扩展方法——类型转换

    1. ObjToBool:object型转换为bool型
    2. StrToBool:string型转换为bool型
    3. ObjToInt:将对象转换为Int32类型
    4. StrToInt:将字符串转换为Int32类型
    5. StrToIntNull
    6. ObjToDecimal
    7. StrToDecimal
    8. StrToDecimalNull
    9. ObjToDouble
    10. StrToDouble
    11. StrToGuid
    12. StrToDateTime
    13. ObjToDateTime
    14. ObjToStr
            #region 类型转换
    
            /// <summary>
            /// object型转换为bool型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的bool类型结果</returns>
            public static bool ObjToBool(this object expression, bool defValue)
            {
                if (expression != null)
                    return ObjToBool(expression, defValue);
    
                return defValue;
            }
    
            /// <summary>
            /// string型转换为bool型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的bool类型结果</returns>
            public static bool StrToBool(this string expression, bool defValue)
            {
                if (expression != null)
                {
                    if (string.Compare(expression, "true", true) == 0 || expression == "")
                        return true;
                    else if (string.Compare(expression, "false", true) == 0 || expression == "")
                        return false;
                }
                return defValue;
            }
    
            /// <summary>
            /// 将对象转换为Int32类型
            /// </summary>
            /// <param name="expression">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static int ObjToInt(this object expression, int defValue)
            {
                if (expression != null)
                    return StrToInt(expression.ToString(), defValue);
    
                return defValue;
            }
    
            /// <summary>
            /// 将字符串转换为Int32类型
            /// </summary>
            /// <param name="expression">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static int StrToInt(this string expression, int defValue)
            {
                if (expression == null)
                    return defValue;
    
                int val;
                if (Int32.TryParse(expression.Replace(",", ""), out val))
                {
                    return val;
                }
                return defValue;
            }
            public static int? StrToIntNull(this string expression)
            {
                if (expression == null)
                    return null;
    
                int val;
                if (Int32.TryParse(expression.Replace(",", ""), out val))
                {
                    return val;
                }
                return null;
            }
    
    
            /// <summary>
            /// 将字符串转换为Int32类型
            /// </summary>
            /// <param name="expression">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static int? StrToInt(this string expression)
            {
                if (expression == null)
                    return null;
    
                int val;
                if (Int32.TryParse(expression.Replace(",", ""), out val))
                {
                    return val;
                }
                return null;
            }
    
            /// <summary>
            /// Object型转换为decimal型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的decimal类型结果</returns>
            public static decimal ObjToDecimal(this object expression, decimal defValue)
            {
                if (expression != null)
                    return StrToDecimal(expression.ToString(), defValue);
    
                return defValue;
            }
    
            /// <summary>
            /// string型转换为decimal型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的decimal类型结果</returns>
            public static decimal StrToDecimal(this string expression, decimal defValue)
            {
                if (expression == null)
                    return defValue;
    
                decimal val;
                if (decimal.TryParse(expression.Replace(",", ""), out val))
                {
                    return val;
                }
                return defValue;
            }
    
            public static decimal? StrToDecimalNull(this string expression)
            {
                if (expression == null)
                    return null;
    
                decimal val;
                if (decimal.TryParse(expression.Replace(",", ""), out val))
                {
                    return val;
                }
                return null;
            }
            /// <summary>
            /// Object型转换为double型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static double ObjToDouble(this object expression, double defValue)
            {
                if (expression != null)
                    return StrToDouble(expression.ToString(), defValue);
    
                return defValue;
            }
    
            /// <summary>
            /// 将字符串转换为Double类型
            /// </summary>
            /// <param name="expression">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static double StrToDouble(this string expression, int defValue)
            {
                if (expression == null)
                    return defValue;
    
                double val;
                if (Double.TryParse(expression.Replace(",", ""), out val))
                {
                    return val;
                }
                return defValue;
            }
    
            /// <summary>
            /// string型转换为double型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static double StrToDouble(this string expression, double defValue)
            {
                if (expression == null)
                    return defValue;
    
                double val;
                if (double.TryParse(expression.Replace(",", ""), out val))
                {
                    return val;
                }
                return defValue;
            }
    
            /// <summary>
            /// string型转换为Guid型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <returns>转换后的int类型结果</returns>
            public static Guid? StrToGuid(this string expression)
            {
                if (expression == null)
                    return null;
    
                Guid val;
                if (Guid.TryParse(expression, out val))
                {
                    return val;
                }
                return null;
            }
    
            /// <summary>
            /// 将对象转换为日期时间类型
            /// </summary>
            /// <param name="str">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static DateTime? StrToDateTime(this string str, DateTime? defValue)
            {
                if (!string.IsNullOrEmpty(str))
                {
                    DateTime dateTime;
                    if (DateTime.TryParse(str, out dateTime))
                        return dateTime;
                }
                return defValue;
            }
    
            /// <summary>
            /// 将对象转换为日期时间类型
            /// </summary>
            /// <param name="str">要转换的字符串</param>
            /// <returns>转换后的int类型结果</returns>
            public static DateTime? StrToDateTime(this string str)
            {
                return StrToDateTime(str, null);
            }
    
            /// <summary>
            /// 将对象转换为日期时间类型
            /// </summary>
            /// <param name="obj">要转换的对象</param>
            /// <returns>转换后的int类型结果</returns>
            public static DateTime? ObjToDateTime(this object obj)
            {
                return StrToDateTime(obj.ToString());
            }
    
            /// <summary>
            /// 将对象转换为日期时间类型
            /// </summary>
            /// <param name="obj">要转换的对象</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static DateTime? ObjToDateTime(this object obj, DateTime defValue)
            {
                return StrToDateTime(obj.ToString(), defValue);
            }
    
            /// <summary>
            /// 将对象转换为字符串
            /// </summary>
            /// <param name="obj">要转换的对象</param>
            /// <returns>转换后的string类型结果</returns>
            public static string ObjToStr(this object obj)
            {
                if (obj == null)
                    return "";
                return obj.ToString().Trim();
            }
    
            #endregion 对象转换处理
     
  • 相关阅读:
    TestNG DataProvider的几种方法写法
    ruby操作EXCEL的简单示例
    QTP的tsr对象库文件转换成XML
    Ruby对时间的处理
    java读取YAML文件
    ruby遍历文件夹
    ruby操作excel文件
    [转载]利用ruby的Net::HTTP发起http请求并对返回包进行简单的校验
    QTP连接MySQL (转载)
    Ruby 冒泡排序
  • 原文地址:https://www.cnblogs.com/amusement1992/p/13496051.html
Copyright © 2020-2023  润新知