当前位置: 首页 > news >正文

【.Net 6.0--通用帮助类--ConvertHelper】

前言

类型转换帮助类,包含下表中的方法:

方法名方法解释
ObjToIntobject转int
ObjToMoneyobject转double
ObjToStringobject转string
ObjToDecimalobject转decimal
ObjToDateobject转datetime
ObjToDateSplitYMDobject转datetime(yyyy-MM-dd)
ObjToDateSplitYMDHMSobject转datetime(yyyy-MM-dd HH:mm:ss)
ObjToDateYobject转datetime(yyyy)
ObjToDateYMDobject转datetime(yyyyMMdd)
ObjToDateYMDHobject转datetime(yyyyMMddHH)
ObjToDateYMDHMSobject转datetime(yyyyMMddHHmmss)
ObjToBoolobject转bool
ObjToCharobject转char
ObjToStringBySplitobject转list
ObjToBase64Stringobject转base64 string
ObjToStringFromBase64base64 string转object

代码示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace VW.API.Common.Utils
{/// <summary>/// ConvertHelper 的摘要说明:格式转换帮助类/// </summary>public static class ConvertHelper{/// <summary>/// obj->int/// </summary>/// <param name="thisValue"></param>/// <returns>int</returns>public static int ObjToInt(this object thisValue){try{if (thisValue == null){return 0;}if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out int reval)){return reval;}return 0;}catch (Exception) { throw; }}/// <summary>/// obj->double/// </summary>/// <param name="thisValue"></param>/// <returns>double</returns>public static double ObjToMoney(this object thisValue){try{if (thisValue == null){return 0;}if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out double reval)){return reval;}return 0;}catch (Exception) { throw; }}/// <summary>/// obj->string/// </summary>/// <param name="thisValue"></param>/// <returns>string</returns>public static string ObjToString(this object thisValue){try{if (thisValue == null){return "";}if (thisValue != null)return thisValue.ToString();return "";}catch (Exception) { throw; }}/// <summary>/// obj->decimal/// </summary>/// <param name="thisValue"></param>/// <returns>decimal</returns>public static decimal ObjToDecimal(this object thisValue){try{if (thisValue == null){return 0;}if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out decimal reval)){return reval;}return 0;}catch (Exception) { throw; }}/// <summary>/// obj->datetime2/// </summary>/// <param name="thisValue"></param>/// <returns>DateTime</returns>public static DateTime ObjToDate2(this object thisValue){try{return thisValue.ToString().Length switch{//年4 => new DateTime(thisValue.ObjToInt(), 1, 1, 0, 0, 0),//月6 => new DateTime(thisValue.ToString().Substring(0, 4).ObjToInt(), thisValue.ToString().Substring(4, 2).ObjToInt(), 1, 0, 0, 0),//日8 => new DateTime(thisValue.ToString().Substring(0, 4).ObjToInt(), thisValue.ToString().Substring(4, 2).ObjToInt(), thisValue.ToString().Substring(6, 2).ObjToInt(), 0, 0, 0),//时10 => new DateTime(thisValue.ToString().Substring(0, 4).ObjToInt(), thisValue.ToString().Substring(4, 2).ObjToInt(), thisValue.ToString().Substring(6, 2).ObjToInt(), thisValue.ToString().Substring(8, 2).ObjToInt(), 0, 0),//分钟12 => new DateTime(thisValue.ToString().Substring(0, 4).ObjToInt(), thisValue.ToString().Substring(4, 2).ObjToInt(), thisValue.ToString().Substring(6, 2).ObjToInt(), thisValue.ToString().Substring(8, 2).ObjToInt(), thisValue.ToString().Substring(10, 2).ObjToInt(), 0),_ => DateTime.MinValue,};}catch (Exception) { throw; }}/// <summary>/// obj->datetime/// </summary>/// <param name="thisValue"></param>/// <returns>DateTime</returns>public static DateTime ObjToDate(this object thisValue){try{DateTime reval = DateTime.MinValue;if (thisValue == null){return reval;}if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval)){reval = Convert.ToDateTime(thisValue);}return reval;}catch (Exception) { throw; }}/// <summary>/// obj->datetime(SplitYMD)/// </summary>/// <param name="thisValue"></param>/// <returns>DateTime</returns>public static string ObjToDateSplitYMD(this object thisValue){try{if (thisValue == null){return "";}return ObjToDate(thisValue).ToString("yyyy-MM-dd");}catch (Exception) { throw; }}/// <summary>/// obj->datetime(SplitYMDHMS)/// </summary>/// <param name="thisValue"></param>/// <returns>DateTime</returns>public static string ObjToDateSplitYMDHMS(this object thisValue){try{if (thisValue == null){return "";}return ObjToDate(thisValue).ToString("yyyy-MM-dd HH:mm:ss");}catch (Exception) { throw; }}/// <summary>/// obj->datetime(Y)/// </summary>/// <param name="thisValue"></param>/// <returns>DateTime</returns>public static string ObjToDateY(this object thisValue){try{if (thisValue == null){return "";}return ObjToDate(thisValue).ToString("yyyy");}catch (Exception) { throw; }}/// <summary>/// obj->datetime(YMD)/// </summary>/// <param name="thisValue"></param>/// <returns>DateTime</returns>public static string ObjToDateYMD(this object thisValue){try{if (thisValue == null){return "";}return ObjToDate(thisValue).ToString("yyyyMMdd");}catch (Exception) { throw; }}/// <summary>/// obj->datetime(YMDH)/// </summary>/// <param name="thisValue"></param>/// <returns>DateTime</returns>public static string ObjToDateYMDH(this object thisValue){try{if (thisValue == null){return "";}return ObjToDate(thisValue).ToString("yyyyMMddHH");}catch (Exception) { throw; }}/// <summary>/// obj->datetime(YMDHMS)/// </summary>/// <param name="thisValue"></param>/// <returns>DateTime</returns>public static string ObjToDateYMDHMS(this object thisValue){try{if (thisValue == null){return "";}return ObjToDate(thisValue).ToString("yyyyMMddHHmmss");}catch (Exception) { throw; }}/// <summary>/// obj->bool/// </summary>/// <param name="thisValue"></param>/// <returns>bool</returns>public static bool ObjToBool(this object thisValue){try{if (thisValue == null){return false;}if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out bool reval)){return reval;}return false;}catch (Exception) { throw; }}/// <summary>/// obj->char/// </summary>/// <param name="thisValue"></param>/// <returns>char</returns>public static char ObjToChar(this object thisValue){try{if (thisValue == null){return ' ';}if (thisValue != null && thisValue != DBNull.Value && char.TryParse(thisValue.ToString(), out char reval)){return reval;}return ' ';}catch (Exception) { throw; }}/// <summary>/// obj->List<string>/// </summary>/// <param name="thisValue"></param>/// <param name="split"></param>/// <returns>List<string></returns>public static List<string> ObjToStringBySplit(this object thisValue, char[] split = null){try{if (thisValue == null){return new List<string>();}if (split == null || split.Length == 0){split = new char[] { ';', ';', ',', ',', ' ' };}return thisValue.ToString().Split(split).ToList();}catch (Exception) { throw; }}/// <summary>/// obj->string<string>/// </summary>/// <param name="thisValue"></param>/// <param name="split"></param>/// <returns>List<string></returns>public static string ObjToStringByReplace(this object thisValue, string[] replace = null){try{if (thisValue == null){return "";}//-()&"*%()if (replace == null || replace.Length == 0){replace = new string[] { "-", "(", ")", "&", "\"", "*", "%", "(", ")", "_" };}string returnValue = thisValue.ToString();foreach (string r in replace){returnValue = returnValue.Replace(r, $"\\{r}");}return returnValue;}catch (Exception) { throw; }}/// <summary>/// obj->base64string/// </summary>/// <param name="thisValue"></param>/// <returns>string</returns>public static string ObjToBase64String(this object thisValue){try{if (thisValue == null){return "";}if (thisValue != null)return Convert.ToBase64String(Encoding.UTF8.GetBytes($"{thisValue}"));return "";}catch (Exception) { throw; }}/// <summary>/// obj->string/// </summary>/// <param name="thisValue"></param>/// <returns>string</returns>public static string ObjToStringFromBase64(this object thisValue){try{if (thisValue == null){return "";}if (thisValue != null)return System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(thisValue.ToString()));return "";}catch (Exception) { throw; }}}
}
http://www.lryc.cn/news/262088.html

相关文章:

  • 【加解密】报文签名与加解密,MD5,RSA,AES使用案例(基于 Java)
  • 新建vue3项目
  • 出现 Error:Unable to access jarfile xxxx\target\nacos-server.jar 解决方法
  • 记录一次API报文替换点滴
  • PMP项目管理 - 沟通管理
  • fckeditor编辑器改造示例:增加PRE,CODE控件
  • 风速预测(五)基于Pytorch的EMD-CNN-LSTM模型
  • 单元测试二(理论)-云计算2023.12-云南农业大学
  • QModelIndex 是 Qt 框架中的一个类,用于表示数据模型中的索引位置
  • 前端实现一个时间区间内,再次单选功能,使用Antd组件库内日历组件Calendar
  • 【运维笔记】Hyperf正常情况下Xdebug报错死循环解决办法
  • 嵌入式开发中的总线与时钟
  • k8s debug 浅谈
  • Day10 Liunx高级系统设计11-数据库2
  • 车载导航系统UI界面,可视化大屏设计(PS源文件)
  • 工作之踩坑记录
  • 【深度学习目标检测】四、基于深度学习的抽烟识别(python,yolov8)
  • YML学习
  • 华为HCIP认证H12-821题库下
  • 01--二分查找
  • 初识大数据应用,一文掌握大数据知识文集(1)
  • Kafka生产问题总结及性能优化实践
  • [MySQL]数据库原理2,Server,DataBase,Connection,latin1、UTF-8,gb2312,Encoding,Default Collation——喵喵期末不挂科
  • 【算法集训】基础数据结构:十、矩阵
  • python排序算法 直接插入排序法和折半插入排序法
  • 【flutter对抗】blutter使用+ACTF习题
  • OpenHarmony 如何去除系统锁屏应用
  • Python - 搭建 Flask 服务实现图像、视频修复需求
  • C#基础——构造函数、析构函数
  • jmeter 如何循环使用接口返回的多值?