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

}

相关推荐
你怎么知道我是队长4 小时前
C语言---枚举变量
c语言·开发语言
李慕婉学姐4 小时前
【开题答辩过程】以《基于JAVA的校园即时配送系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·开发语言·数据库
吃茄子的猫4 小时前
quecpython中&的具体含义和使用场景
开发语言·python
云栖梦泽5 小时前
易语言中小微企业Windows桌面端IoT监控与控制
开发语言
数据大魔方5 小时前
【期货量化实战】日内动量策略:顺势而为的短线交易法(Python源码)
开发语言·数据库·python·mysql·算法·github·程序员创富
奋进的芋圆6 小时前
Java 延时任务实现方案详解(适用于 Spring Boot 3)
java·spring boot·redis·rabbitmq
李建军6 小时前
界止签章宗地号替换工具
c#
qq_425263326 小时前
.net开发框架和语言
c#
sxlishaobin6 小时前
设计模式之桥接模式
java·设计模式·桥接模式
Edward.W6 小时前
Python uv:新一代Python包管理工具,彻底改变开发体验
开发语言·python·uv