10 changed files with 505 additions and 14 deletions
@ -0,0 +1,412 @@ |
|||
using System; |
|||
using System.ComponentModel; |
|||
using System.Reflection; |
|||
|
|||
namespace Wood.Util.ExtensionMethods |
|||
{ |
|||
public static class TypeConvertExtension |
|||
{ |
|||
#region string
|
|||
public static int? TryToInt(this string p_str) |
|||
{ |
|||
int resu; |
|||
bool isSucc = int.TryParse(p_str, out resu); |
|||
if (isSucc) |
|||
{ |
|||
return resu; |
|||
} |
|||
else |
|||
{ |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static int TryToIntZero(this string p_str) |
|||
{ |
|||
return p_str.TryToInt() ?? 0; |
|||
} |
|||
|
|||
public static double? TryToDouble(this string p_str) |
|||
{ |
|||
double resu; |
|||
bool isSucc = double.TryParse(p_str, out resu); |
|||
if (isSucc) |
|||
{ |
|||
return resu; |
|||
} |
|||
else |
|||
{ |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static double TryToDoubleZero(this string p_str) |
|||
{ |
|||
return p_str.TryToDouble() ?? 0; |
|||
} |
|||
|
|||
public static DateTime? TryToDateTime(this string p_str) |
|||
{ |
|||
if (p_str == null) |
|||
{ |
|||
return null; |
|||
} |
|||
string str = p_str.ToString(); |
|||
DateTime resu; |
|||
bool isSucc = DateTime.TryParse(str, out resu); |
|||
if (isSucc) |
|||
{ |
|||
return resu; |
|||
} |
|||
else |
|||
{ |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static DateTime TryToDateTime1900(this string p_str) |
|||
{ |
|||
var ret = p_str.TryToDateTime(); |
|||
if (ret == null) |
|||
{ |
|||
return Convert.ToDateTime("1900-01-01"); |
|||
} |
|||
if (ret < Convert.ToDateTime("1900-01-01")) |
|||
{ |
|||
return Convert.ToDateTime("1900-01-01"); |
|||
} |
|||
else |
|||
{ |
|||
return (DateTime)ret; |
|||
} |
|||
} |
|||
|
|||
|
|||
public static bool? TryToBool(this string p_str) |
|||
{ |
|||
if (p_str == null) |
|||
{ |
|||
return null; |
|||
} |
|||
bool ret; |
|||
bool isSucc = Boolean.TryParse(p_str, out ret); |
|||
if (isSucc) |
|||
{ |
|||
return ret; |
|||
} |
|||
else |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
} |
|||
|
|||
public static decimal? TryToDecimal(this string p_str) |
|||
{ |
|||
if (p_str == null) |
|||
{ |
|||
return null; |
|||
} |
|||
decimal ret; |
|||
bool isSucc = Decimal.TryParse(p_str, out ret); |
|||
if (isSucc) |
|||
{ |
|||
return ret; |
|||
} |
|||
else |
|||
{ |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static decimal TryToDecimalZero(this string p_str) |
|||
{ |
|||
return p_str.TryToDecimal() ?? 0.0M; |
|||
} |
|||
|
|||
#endregion
|
|||
|
|||
#region object
|
|||
public static int? TryToInt(this object p_obj) |
|||
{ |
|||
if (p_obj == null) |
|||
{ |
|||
return null; |
|||
} |
|||
string str = p_obj?.ToString(); |
|||
int resu; |
|||
bool isSucc = int.TryParse(str, out resu); |
|||
if (isSucc) |
|||
{ |
|||
return resu; |
|||
} |
|||
else |
|||
{ |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static int TryToIntZero(this object p_obj) |
|||
{ |
|||
int? ret = TryToInt(p_obj); |
|||
return ret ?? 0; |
|||
} |
|||
|
|||
public static double? TryToDouble(this object p_obj) |
|||
{ |
|||
if (p_obj == null) |
|||
{ |
|||
return null; |
|||
} |
|||
string str = p_obj?.ToString(); |
|||
double resu; |
|||
bool isSucc = double.TryParse(str, out resu); |
|||
if (isSucc) |
|||
{ |
|||
return resu; |
|||
} |
|||
else |
|||
{ |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static double TryToDoubleZero(this object p_obj) |
|||
{ |
|||
return p_obj.TryToDouble() ?? 0; |
|||
} |
|||
|
|||
public static DateTime? TryToDateTime(this object p_obj) |
|||
{ |
|||
if (p_obj == null) |
|||
{ |
|||
return null; |
|||
} |
|||
string str = p_obj?.ToString(); |
|||
DateTime resu; |
|||
bool isSucc = DateTime.TryParse(str, out resu); |
|||
if (isSucc) |
|||
{ |
|||
return resu; |
|||
} |
|||
else |
|||
{ |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static DateTime TryToDateTime1900(this object p_obj) |
|||
{ |
|||
var ret = p_obj.TryToDateTime(); |
|||
if (ret == null) |
|||
{ |
|||
return Convert.ToDateTime("1900-01-01"); |
|||
} |
|||
if (ret < Convert.ToDateTime("1900-01-01")) |
|||
{ |
|||
return Convert.ToDateTime("1900-01-01"); |
|||
} |
|||
else |
|||
{ |
|||
return (DateTime)ret; |
|||
} |
|||
} |
|||
|
|||
public static bool? TryToBool(this object p_obj) |
|||
{ |
|||
if (p_obj == null) |
|||
{ |
|||
return null; |
|||
} |
|||
string str = p_obj?.ToString(); |
|||
bool resu; |
|||
bool isSucc = Boolean.TryParse(str, out resu); |
|||
if (isSucc) |
|||
{ |
|||
return resu; |
|||
} |
|||
else |
|||
{ |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static decimal? TryToDecimal(this object p_obj) |
|||
{ |
|||
if (p_obj == null) |
|||
{ |
|||
return null; |
|||
} |
|||
string str = p_obj?.ToString(); |
|||
decimal ret; |
|||
bool isSucc = Decimal.TryParse(str, out ret); |
|||
if (isSucc) |
|||
{ |
|||
return ret; |
|||
} |
|||
else |
|||
{ |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static decimal TryToDecimalZero(this object p_obj) |
|||
{ |
|||
return p_obj.TryToDecimal() ?? 0.0M; |
|||
} |
|||
#endregion
|
|||
|
|||
public static bool HasValue(this string p_str) |
|||
{ |
|||
return string.IsNullOrEmpty(p_str?.Trim()) ? false : true; |
|||
} |
|||
|
|||
public static bool IsNullOrEmpty(this string p_str) |
|||
{ |
|||
return string.IsNullOrEmpty(p_str?.Trim()) ? true : false; |
|||
} |
|||
|
|||
public static string TryToEmptyIfNull(this string p_str) |
|||
{ |
|||
if (p_str == null) |
|||
{ |
|||
return string.Empty; |
|||
} |
|||
else |
|||
{ |
|||
return p_str; |
|||
} |
|||
} |
|||
|
|||
public static string Left(this string str, int len) |
|||
{ |
|||
string result = str.Substring(0, len); |
|||
return result; |
|||
} |
|||
|
|||
public static string Right(this string str, int len) |
|||
{ |
|||
if (str.Length < len) |
|||
{ |
|||
return str; |
|||
} |
|||
string result = str.Substring(str.Length - len, len); |
|||
return result; |
|||
} |
|||
|
|||
public static object IntDBNull(this object p_obj) |
|||
{ |
|||
if (p_obj == null) |
|||
{ |
|||
return DBNull.Value; |
|||
} |
|||
string str = p_obj?.ToString(); |
|||
int resu; |
|||
bool isSucc = int.TryParse(str, out resu); |
|||
if (isSucc) |
|||
{ |
|||
return resu; |
|||
} |
|||
else |
|||
{ |
|||
return DBNull.Value; |
|||
} |
|||
} |
|||
|
|||
public static object DoubleDBNull(this object p_obj) |
|||
{ |
|||
if (p_obj == null) |
|||
{ |
|||
return DBNull.Value; |
|||
} |
|||
string str = p_obj?.ToString(); |
|||
double resu; |
|||
bool isSucc = double.TryParse(str, out resu); |
|||
if (isSucc) |
|||
{ |
|||
return resu; |
|||
} |
|||
else |
|||
{ |
|||
return DBNull.Value; |
|||
} |
|||
} |
|||
|
|||
public static object DateTimeDBNull(this object p_obj) |
|||
{ |
|||
if (p_obj == null) |
|||
{ |
|||
return DBNull.Value; |
|||
} |
|||
string str = p_obj?.ToString(); |
|||
DateTime resu; |
|||
bool isSucc = DateTime.TryParse(str, out resu); |
|||
if (isSucc) |
|||
{ |
|||
return resu; |
|||
} |
|||
else |
|||
{ |
|||
return DBNull.Value; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 返回枚举项的描述信息。
|
|||
/// </summary>
|
|||
/// <param name="value">要获取描述信息的枚举项。</param>
|
|||
/// <returns>枚举想的描述信息。
|
|||
/// </returns>
|
|||
public static string GetDescription(this Enum value, bool isTop = false) |
|||
{ |
|||
Type enumType = value.GetType(); |
|||
DescriptionAttribute attr = null; |
|||
if (isTop) |
|||
{ |
|||
attr = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute)); |
|||
} |
|||
else |
|||
{ |
|||
// 获取枚举常数名称。
|
|||
string name = Enum.GetName(enumType, value); |
|||
if (name != null) |
|||
{ |
|||
// 获取枚举字段。
|
|||
FieldInfo fieldInfo = enumType.GetField(name); |
|||
if (fieldInfo != null) |
|||
{ |
|||
// 获取描述的属性。
|
|||
attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute; |
|||
} |
|||
} |
|||
} |
|||
if (attr != null && !string.IsNullOrEmpty(attr.Description)) |
|||
return attr.Description; |
|||
else |
|||
return string.Empty; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 23:59:59
|
|||
/// </summary>
|
|||
/// <param name="p_dt"></param>
|
|||
/// <returns></returns>
|
|||
public static DateTime ToMaxTime(this DateTime p_dt) |
|||
{ |
|||
string str = p_dt.ToString("yyyy-MM-dd 23:59:59"); |
|||
return Convert.ToDateTime(str); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 00:00:01
|
|||
/// </summary>
|
|||
/// <param name="p_dt"></param>
|
|||
/// <returns></returns>
|
|||
public static DateTime ToMinTime(this DateTime p_dt) |
|||
{ |
|||
string str = p_dt.ToString("yyyy-MM-dd 00:00:01"); |
|||
return Convert.ToDateTime(str); |
|||
} |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue