.net扩展方法——类型转换
- ObjToBool:object型转换为bool型
- StrToBool:string型转换为bool型
- ObjToInt:将对象转换为Int32类型
- StrToInt:将字符串转换为Int32类型
- StrToIntNull
- ObjToDecimal
- StrToDecimal
- StrToDecimalNull
- ObjToDouble
- StrToDouble
- StrToGuid
- StrToDateTime
- ObjToDateTime
- 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 对象转换处理