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

}

相关推荐
玩泥巴的17 小时前
存储那么贵,何不白嫖飞书云文件空间
c#·.net·二次开发·飞书
炸膛坦客18 小时前
单片机/C/C++八股:(二十)指针常量和常量指针
c语言·开发语言·c++
兑生18 小时前
【灵神题单·贪心】1481. 不同整数的最少数目 | 频率排序贪心 | Java
java·开发语言
daidaidaiyu18 小时前
一文学习 Spring 声明式事务源码全流程总结
java·spring
炸膛坦客19 小时前
单片机/C/C++八股:(十九)栈和堆的区别?
c语言·开发语言·c++
零雲19 小时前
java面试:了解抽象类与接口么?讲一讲它们的区别
java·开发语言·面试
Jay_Franklin19 小时前
Quarto与Python集成使用
开发语言·python·markdown
2401_8318249620 小时前
代码性能剖析工具
开发语言·c++·算法
是wzoi的一名用户啊~20 小时前
【C++小游戏】2048
开发语言·c++
Sunshine for you21 小时前
C++中的职责链模式实战
开发语言·c++·算法