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

前言

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

方法名 方法解释
ObjToInt object转int
ObjToMoney object转double
ObjToString object转string
ObjToDecimal object转decimal
ObjToDate object转datetime
ObjToDateSplitYMD object转datetime(yyyy-MM-dd)
ObjToDateSplitYMDHMS object转datetime(yyyy-MM-dd HH:mm:ss)
ObjToDateY object转datetime(yyyy)
ObjToDateYMD object转datetime(yyyyMMdd)
ObjToDateYMDH object转datetime(yyyyMMddHH)
ObjToDateYMDHMS object转datetime(yyyyMMddHHmmss)
ObjToBool object转bool
ObjToChar object转char
ObjToStringBySplit object转list
ObjToBase64String object转base64 string
ObjToStringFromBase64 base64 string转object

代码示例

C# 复制代码
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; }
        }
    }
}
相关推荐
李小星同志4 分钟前
高级算法设计与分析 学习笔记6 B树
笔记·学习
霜晨月c16 分钟前
MFC 使用细节
笔记·学习·mfc
鸽芷咕19 分钟前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
Jhxbdks28 分钟前
C语言中的一些小知识(二)
c语言·开发语言·笔记
java66666888828 分钟前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存29 分钟前
源码分析:LinkedList
java·开发语言
小江湖199429 分钟前
元数据保护者,Caesium压缩不丢重要信息
运维·学习·软件需求·改行学it
代码雕刻家31 分钟前
数据结构-3.1.栈的基本概念
c语言·开发语言·数据结构
Fan_web32 分钟前
JavaScript高级——闭包应用-自定义js模块
开发语言·前端·javascript·css·html
Crazy Struggle37 分钟前
.NET 7+Angular 4 轻量级新零售进销存系统
.net·angular·进销存系统