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

}

相关推荐
OxYGC7 小时前
[玩转GoLang] 5分钟整合Gin / Gorm框架入门
开发语言·golang·gin
锐策7 小时前
Lua 核心知识点详解
开发语言·lua
你三大爷7 小时前
再探volatile原理
java
2301_781668617 小时前
Redis 面试
java·redis·面试
郑洁文8 小时前
基于SpringBoot的天气预报系统的设计与实现
java·spring boot·后端·毕设
kyle~8 小时前
C/C++---动态内存管理(new delete)
c语言·开发语言·c++
沃夫上校8 小时前
MySQL 中文拼音排序问题
java·mysql
Dcs8 小时前
用 Python UTCP 直调 HTTP、CLI、MCP……
java
落日沉溺于海8 小时前
React From表单使用Formik和yup进行校验
开发语言·前端·javascript
快乐肚皮8 小时前
fencing token机制
java·fencing token