当前位置: 首页 > 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; }}}
}

相关文章:

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

前言 类型转换帮助类&#xff0c;包含下表中的方法&#xff1a; 方法名方法解释ObjToIntobject转intObjToMoneyobject转doubleObjToStringobject转stringObjToDecimalobject转decimalObjToDateobject转datetimeObjToDateSplitYMDobject转datetime&#xff08;yyyy-MM-dd&…...

【加解密】报文签名与加解密,MD5,RSA,AES使用案例(基于 Java)

需要考虑哪些问题&#xff1f; 在进行报文传输时&#xff0c;有两个问题需要考虑&#xff1a; 消息防篡改加密报文 定义消息结构 为了方便后面使用&#xff0c;这里定义消息结构&#xff1a; public static class Message {public String data; //消息public String sign;…...

新建vue3项目

三种方法 一. 第一种方式 1、操作步骤&#xff1a; 创建项目目录 vue create 项目名称选择配置方式 ? Please pick a preset: #选择一个配置 Default &#xff08;[Vue 3] babel, eslint&#xff09;Default &#xff08;[Vue 2] babel, eslint&#xff09;Manually select …...

出现 Error:Unable to access jarfile xxxx\target\nacos-server.jar 解决方法

目录 1. 问题所示2. 原理分析3. 解决方法1. 问题所示 执行Nacos中的startup.cmd的时候出现闪退,于是在该脚本的最后一行添加pause,查看因为什么原因闪退 出现的bug如下所示:Error:Unable to access jarfile xxxx\target\nacos-server.jar 截图如下所示: 查看内部文件夹,…...

记录一次API报文替换点滴

1. 需求 各位盆友在日常开发中&#xff0c;有没有遇到上游接口突然不合作了&#xff0c;临时需要切换其他接口的情况&#xff1f;这不巧了&#xff0c;博主团队近期遇到了&#xff0c;又尴尬又忐忑。 尴尬的是临时通知不合作了&#xff0c;事前没有任何提醒&#xff1b; 忐忑…...

PMP项目管理 - 沟通管理

系列文章目录 PMP项目管理 - 质量管理 PMP项目管理 - 采购管理 PMP项目管理 - 资源管理 PMP项目管理 - 风险管理 现在的一切都是为将来的梦想编织翅膀&#xff0c;让梦想在现实中展翅高飞。 Now everything is for the future of dream weaving wings, let the dream fly in…...

fckeditor编辑器改造示例:增加PRE,CODE控件

查看专栏目录 Network 灰鸽宝典专栏主要关注服务器的配置&#xff0c;前后端开发环境的配置&#xff0c;编辑器的配置&#xff0c;网络服务的配置&#xff0c;网络命令的应用与配置&#xff0c;windows常见问题的解决等。 文章目录 修改方法&#xff1a;1&#xff09;修改fckco…...

风速预测(五)基于Pytorch的EMD-CNN-LSTM模型

目录 前言 1 风速数据EMD分解与可视化 1.1 导入数据 1.2 EMD分解 2 数据集制作与预处理 2.1 先划分数据集&#xff0c;按照8&#xff1a;2划分训练集和测试集 2.2 设置滑动窗口大小为96&#xff0c;制作数据集 3 基于Pytorch的EMD-CNN-LSTM模型预测 3.1 数据加载&…...

单元测试二(理论)-云计算2023.12-云南农业大学

文章目录 一、单选题1、三次握手、四次挥手发生在网络模型的哪一层上&#xff1f;2、互联网Internet的拓扑结构是什么&#xff1f;3、以下哪一种网络设备是工作在网络层的&#xff1f;4、以下哪种关于分组交换网络的说法是错误的&#xff1f;5、以下哪种协议是在TCP/IP模型中的…...

QModelIndex 是 Qt 框架中的一个类,用于表示数据模型中的索引位置

QModelIndex 是 Qt 框架中的一个类&#xff0c;用于表示数据模型中的索引位置。 在 Qt 中&#xff0c;数据模型是一种组织和管理数据的方式&#xff0c;常见的数据模型包括 QAbstractItemModel、QStandardItemModel 和 QSqlQueryModel 等。QModelIndex 类提供了一种标识数据模…...

前端实现一个时间区间内,再次单选功能,使用Antd组件库内日历组件Calendar

需求&#xff1a;需要先让用户选择一个时间区间&#xff0c;然后再这个时间区间中&#xff0c;让用户再次去单选其种特殊日期。 思路&#xff1a; 1.先用Antd组件库中日期选择DatePicker.RangePicker实现让用户选择时间区间 2.在选择完时间区间后&#xff0c;用这个时间区间…...

【运维笔记】Hyperf正常情况下Xdebug报错死循环解决办法

问题描述 在使用hyperf进行数据库迁移时&#xff0c;迁移报错&#xff1a; 查看报错信息&#xff0c;错误描述是Xdebug检测到死循环&#xff0c;可是打印的堆栈确实正常堆栈&#xff0c;没看到死循环。 寻求解决 gpt 说的跟没说一样。。 google一下 直接把报错信息粘贴上去…...

嵌入式开发中的总线与时钟

总线 AHB总线 AHB的全称是"Advanced High-performance Bus",中文翻译就是"高级高性能总线"。这是一种在计算机系统中用于连接不同硬件组件的总线架构,它可以帮助这些组件之间高效地传输数据和信息。这个总线架构通常用于处理速度较快且对性能要求较高的…...

k8s debug 浅谈

一 k8s debug 浅谈 说明&#xff1a; 本文只是基于对kubectl debug浅显认识总结的知识点,后续实际使用再补充案例 Kubernetes 官方出品调试工具上手指南(无需安装&#xff0c;开箱即用) debug-application 简化 Pod 故障诊断: kubectl-debug 介绍 1.18 版本之前需要自己…...

Day10 Liunx高级系统设计11-数据库2

DQL:数据查询语言 查询全表 select * from 表名; 查询指定列 select 列名 1, 列名 2,… from 表名 ; 条件查询 select * from 表名 where 条件 ; 注意&#xff1a; 条件查询就是在查询时给出 WHERE 子句&#xff0c;在 WHERE 子句中可以使用如下运算符及关键 字&#…...

车载导航系统UI界面,可视化大屏设计(PS源文件)

大屏组件可以让UI设计师的工作更加便捷&#xff0c;使其更高效快速的完成设计任务。现分享车载导航系统科技风蓝黑简约UI界面、车载系统UI主界面、车载系统科技风UI界面、首页车载系统科技感界面界面的大屏Photoshop源文件&#xff0c;开箱即用&#xff01; 若需 更多行业 相关…...

工作之踩坑记录

1.i386架构之atol函数使用导致的业务程序错误&#xff1a; 情景:将框架传递的链接地址采用整形保存传输,在i386架构上导致地址比较大&#xff0c;采用atol转型可能导致数据被截断出现异常。 方案:采用atoll更大的数据类型进行处理即可避免该问题。 2.Json库使用注意long int问…...

【深度学习目标检测】四、基于深度学习的抽烟识别(python,yolov8)

YOLOv8是一种物体检测算法&#xff0c;是YOLO系列算法的最新版本。 YOLO&#xff08;You Only Look Once&#xff09;是一种实时物体检测算法&#xff0c;其优势在于快速且准确的检测结果。YOLOv8在之前的版本基础上进行了一系列改进和优化&#xff0c;提高了检测速度和准确性。…...

YML学习

讲解YML使用场景、语法和解析 1.基础知识1.1 什么是YML1.2 YML优点1.3 YML使用场景 2.YML语法2.1 基础语法2.2 字面量数据类型2.2.1 字符串2.2.2 NULL2.4.5 时间戳&#xff08;timestamp&#xff09; 2.3 对象\MAP类型2.4 数组/List/Set2.4.1 值为基础类型2.4.2 值为对象2.4.3 …...

华为HCIP认证H12-821题库下

26、6.交换技术核心知识 &#xff08;单选题&#xff09;某交换机运行RSTP协议&#xff0c;其相关配置信息如图所示,请根据命令配置情况指出对于Instance 1&#xff0c;该交换机的角色是: A、根交换机 B、非根交换机 C、交换机 D、无法判断 正确答案是&…...

装饰模式(Decorator Pattern)重构java邮件发奖系统实战

前言 现在我们有个如下的需求&#xff0c;设计一个邮件发奖的小系统&#xff0c; 需求 1.数据验证 → 2. 敏感信息加密 → 3. 日志记录 → 4. 实际发送邮件 装饰器模式&#xff08;Decorator Pattern&#xff09;允许向一个现有的对象添加新的功能&#xff0c;同时又不改变其…...

转转集团旗下首家二手多品类循环仓店“超级转转”开业

6月9日&#xff0c;国内领先的循环经济企业转转集团旗下首家二手多品类循环仓店“超级转转”正式开业。 转转集团创始人兼CEO黄炜、转转循环时尚发起人朱珠、转转集团COO兼红布林CEO胡伟琨、王府井集团副总裁祝捷等出席了开业剪彩仪式。 据「TMT星球」了解&#xff0c;“超级…...

第 86 场周赛:矩阵中的幻方、钥匙和房间、将数组拆分成斐波那契序列、猜猜这个单词

Q1、[中等] 矩阵中的幻方 1、题目描述 3 x 3 的幻方是一个填充有 从 1 到 9 的不同数字的 3 x 3 矩阵&#xff0c;其中每行&#xff0c;每列以及两条对角线上的各数之和都相等。 给定一个由整数组成的row x col 的 grid&#xff0c;其中有多少个 3 3 的 “幻方” 子矩阵&am…...

Android Bitmap治理全解析:从加载优化到泄漏防控的全生命周期管理

引言 Bitmap&#xff08;位图&#xff09;是Android应用内存占用的“头号杀手”。一张1080P&#xff08;1920x1080&#xff09;的图片以ARGB_8888格式加载时&#xff0c;内存占用高达8MB&#xff08;192010804字节&#xff09;。据统计&#xff0c;超过60%的应用OOM崩溃与Bitm…...

代理篇12|深入理解 Vite中的Proxy接口代理配置

在前端开发中,常常会遇到 跨域请求接口 的情况。为了解决这个问题,Vite 和 Webpack 都提供了 proxy 代理功能,用于将本地开发请求转发到后端服务器。 什么是代理(proxy)? 代理是在开发过程中,前端项目通过开发服务器,将指定的请求“转发”到真实的后端服务器,从而绕…...

10-Oracle 23 ai Vector Search 概述和参数

一、Oracle AI Vector Search 概述 企业和个人都在尝试各种AI&#xff0c;使用客户端或是内部自己搭建集成大模型的终端&#xff0c;加速与大型语言模型&#xff08;LLM&#xff09;的结合&#xff0c;同时使用检索增强生成&#xff08;Retrieval Augmented Generation &#…...

管理学院权限管理系统开发总结

文章目录 &#x1f393; 管理学院权限管理系统开发总结 - 现代化Web应用实践之路&#x1f4dd; 项目概述&#x1f3d7;️ 技术架构设计后端技术栈前端技术栈 &#x1f4a1; 核心功能特性1. 用户管理模块2. 权限管理系统3. 统计报表功能4. 用户体验优化 &#x1f5c4;️ 数据库设…...

基于TurtleBot3在Gazebo地图实现机器人远程控制

1. TurtleBot3环境配置 # 下载TurtleBot3核心包 mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src git clone -b noetic-devel https://github.com/ROBOTIS-GIT/turtlebot3.git git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git git clone -b noetic-dev…...

iOS性能调优实战:借助克魔(KeyMob)与常用工具深度洞察App瓶颈

在日常iOS开发过程中&#xff0c;性能问题往往是最令人头疼的一类Bug。尤其是在App上线前的压测阶段或是处理用户反馈的高发期&#xff0c;开发者往往需要面对卡顿、崩溃、能耗异常、日志混乱等一系列问题。这些问题表面上看似偶发&#xff0c;但背后往往隐藏着系统资源调度不当…...

GitHub 趋势日报 (2025年06月06日)

&#x1f4ca; 由 TrendForge 系统生成 | &#x1f310; https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日获星趋势图 今日获星趋势图 590 cognee 551 onlook 399 project-based-learning 348 build-your-own-x 320 ne…...