c#各种对象转换

1、转换为String

public static string ConvertString(this Object obj)

{

if (obj == null || obj == System.DBNull.Value)

return string.Empty;

return obj.ToString();

}

2、转换为double

public static double ConvertDouble(this Object obj)

{

double db = 0;

if (obj == null || obj == System.DBNull.Value)

return db;

bool bol = double.TryParse(obj.ToString(), out db);

if (bol)

return db;

else

return 0;

}

3、转换为int

public static int ConvertInt(this Object obj)

{

int m_int = 0;

if (obj == null || obj == System.DBNull.Value)

return m_int;

bool bol = Int32.TryParse(obj.ToString(), out m_int);

if (bol)

return m_int;

else

return 0;

}

4、转换为日期

public static DateTime ConvertDate(this Object obj)

{

DateTime m_date = DateTime.MinValue;

if (obj == null || obj == System.DBNull.Value)

return m_date;

bool bol = DateTime.TryParse(obj.ToString(), out m_date);

if (bol)

return m_date;

else

return DateTime.MinValue;

}

5、转换为时间

public static DateTime ConvertDateTime(this string str)

{

DateItem di = new DateItem();

string date = di.ConvertDateToData(str, "-");

return DateTime.Parse(date);

}

相关推荐
xyq20245 小时前
CSS Backgrounds(背景)
开发语言
Aurorar0rua6 小时前
CS50 x 2024 Notes C - 06
开发语言·学习方法
xyq20246 小时前
SQLite Like 子句详解
开发语言
Highcharts.js6 小时前
线形比赛积分增长或竞赛图|Highcharts企业图表代码示列
开发语言·前端·javascript·折线图·highcharts·竞赛图
古城小栈6 小时前
rust 亿级并发模型,实践完成
开发语言·网络·rust
明如正午6 小时前
【C#】托管调试助手 “PInvokeStackImbalance“:的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。
c#
Codigger官方6 小时前
Phoenix 语言起步指南:开启 Polyglot Singularity 之门
开发语言·人工智能·程序人生
让学习成为一种生活方式6 小时前
大肠杆菌合成扑热息痛--对乙酰氨基酚--文献精读227
开发语言·前端·javascript
yuzhiboyouye6 小时前
为什么sping-boot+ mabatis。写接口时要建好几个VO。各自用途时
java
啧不应该啊6 小时前
Day1 C与python输入输出语句区别
c语言·开发语言