• C#Conver的简单封装(Object>常见类型)


      1 using Newtonsoft.Json;
      2 using System;
      3 using System.Collections.Generic;
      4 using System.Collections.Specialized;
      5 using System.ComponentModel;
      6 using System.Data;
      7 using System.IO;
      8 using System.Reflection;
      9 using System.Text.RegularExpressions;
     10 
     11 namespace Common
     12 {
     13     public class ConverHelper
     14         {
     15             #region object 转换T类型数据
     16             /// <summary>
     17             /// 转换 object为 T 类型
     18             /// </summary>
     19             /// <typeparam name="T">T 类型</typeparam>
     20             /// <param name="obj">object 数据</param>
     21             /// <param name="defVal">默认值</param>
     22             /// <param name="exp">是否抛出异常</param>
     23             /// <returns></returns>
     24             public static T ToTSource<T>(object obj, T defVal = default(T), bool exp = false)
     25             {
     26                 T result = defVal;
     27                 if (obj == null)
     28                 {
     29                     return result;
     30                 }
     31                 if (obj is T)
     32                 {
     33                     return (T)obj;
     34                 }
     35 
     36                 try
     37                 {
     38                     Type conversionType = typeof(T);
     39                     object obj2 = null;
     40                     if (conversionType.Equals(typeof(Guid)))
     41                         obj2 = new Guid(Convert.ToString(obj));
     42                     else
     43                         obj2 = Convert.ChangeType(obj, conversionType);
     44                     result = (T)obj2;
     45                 }
     46                 catch (Exception ex)
     47                 {
     48                     if (exp == true)
     49                     {
     50                         throw ex;
     51                     }
     52                 }
     53                 return result;
     54             }
     55             #endregion
     56 
     57             #region 数值类型转换
     58             #region 转换为 bool 类型
     59             /// <summary>
     60             /// 将object转换为 bool 类型
     61             /// </summary>
     62             /// <param name="obj">待转换的object</param>
     63             /// <param name="defVal">缺省值(转换不成功)</param>
     64             /// <param name="exp">是否抛出异常(默认不抛出)</param>
     65             /// <returns>转换后的bool类型结果</returns>
     66             public static bool ToBool(object obj, bool defVal = false, bool exp = false)
     67             {
     68                 bool result = defVal;
     69                 try
     70                 {
     71                     if (obj != null)
     72                         result = Convert.ToBoolean(obj);
     73                 }
     74                 catch (Exception ex)
     75                 {
     76                     if (exp == true)
     77                     {
     78                         throw ex;
     79                     }
     80                 }
     81                 return result;
     82             }
     83 
     84             #endregion
     85 
     86             #region 转换为 Int 数值类型
     87             /// <summary>
     88             /// 将object转换Int 类型
     89             /// </summary>
     90             /// <param name="str">待转换的object</param>
     91             /// <param name="defVal">缺省值(转换不成功)</param>
     92             /// <param name="exp">是否抛出异常(默认不抛出)</param>
     93             /// <returns>转换后的Int类型结果</returns>
     94             public static int ToInt(object obj, int defVal = 0, bool exp = false)
     95             {
     96                 int result = defVal;
     97                 try
     98                 {
     99                     if (obj != null)
    100                         result = Convert.ToInt32(obj);
    101                 }
    102                 catch (Exception ex)
    103                 {
    104                     if (exp == true)
    105                     {
    106                         throw ex;
    107                     }
    108                 }
    109                 return result;
    110             }
    111 
    112             #endregion
    113 
    114             #region 转换为 Float 数值类型
    115             /// <summary>
    116             /// 将object转换 Float 类型
    117             /// </summary>
    118             /// <param name="str">待转换的object</param>
    119             /// <param name="defVal">缺省值(转换不成功)</param>
    120             /// <param name="exp">是否抛出异常(默认不抛出)</param>
    121             /// <returns>转换后的Int类型结果</returns>
    122             public static float ToFloat(object obj, float defVal = 0, bool exp = false)
    123             {
    124                 float result = defVal;
    125                 try
    126                 {
    127                     if (obj != null)
    128                         result = Convert.ToSingle(obj);
    129                 }
    130                 catch (Exception ex)
    131                 {
    132                     if (exp == true)
    133                     {
    134                         throw ex;
    135                     }
    136                 }
    137                 return result;
    138             }
    139 
    140             #endregion
    141 
    142             #region 转换为 Double 数值类型
    143             /// <summary>
    144             /// 将object转换 double 类型
    145             /// </summary>
    146             /// <param name="str">待转换的object</param>
    147             /// <param name="defVal">缺省值(转换不成功)</param>
    148             /// <param name="exp">是否抛出异常(默认不抛出)</param>
    149             /// <returns>转换后的Int类型结果</returns>
    150             public static double ToDouble(object obj, double defVal = 0, bool exp = false)
    151             {
    152                 double result = defVal;
    153                 try
    154                 {
    155                     if (obj != null)
    156                         result = Convert.ToDouble(obj);
    157                 }
    158                 catch (Exception ex)
    159                 {
    160                     if (exp == true)
    161                     {
    162                         throw ex;
    163                     }
    164                 }
    165                 return result;
    166             }
    167 
    168             #endregion
    169 
    170             #region 转换为 decimal 数值类型
    171             /// <summary>
    172             /// 将对象转换 decimal 类型
    173             /// </summary>
    174             /// <param name="obj">待转换的字符串</param>
    175             /// <param name="defVal">缺省值(转换不成功)</param>
    176             /// <param name="exp">是否抛出异常(默认不抛出)</param>
    177             /// <returns>转换后的decimal类型结果</returns>
    178             public static decimal ToDecimal(object obj, decimal defVal = 0m, bool exp = false)
    179             {
    180                 decimal result = defVal;
    181                 try
    182                 {
    183                     if (obj != null)
    184                         result = Convert.ToDecimal(obj);
    185                 }
    186                 catch (Exception ex)
    187                 {
    188                     if (exp == true)
    189                     {
    190                         throw ex;
    191                     }
    192                 }
    193                 return result;
    194             }
    195             #endregion
    196             #endregion
    197 
    198             #region 转换为 DateTime 日期类型
    199             /// <summary>
    200             /// 将object转换 DateTime 日期类型
    201             /// </summary>
    202             /// <param name="str">待转换的字符串</param>
    203             /// <param name="defVal">缺省值(转换不成功)</param>
    204             /// <param name="exp">是否抛出异常(默认不抛出)</param>
    205             /// <returns>转换后的DateTime类型结果</returns>
    206             public static DateTime ToDateTime(object obj, string defVal = "1970-01-01 08:00:00", bool exp = false)
    207             {
    208                 DateTime result = DateTime.Parse(defVal);
    209                 try
    210                 {
    211                     if (obj != null)
    212                         result = Convert.ToDateTime(obj);
    213                 }
    214                 catch (Exception ex)
    215                 {
    216                     if (exp == true)
    217                     {
    218                         throw ex;
    219                     }
    220                 }
    221                 return result;
    222             }
    223 
    224             #endregion
    225 
    226             ///     将数据转化为 type 类型
    227             /// </summary>
    228             /// <param name="value">要转化的值</param>
    229             /// <param name="type">目标类型</param>
    230             /// <returns>转化为目标类型的 Object 对象</returns>
    231             private static object ChangeType(object value, Type type)
    232             {
    233                 if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
    234                 {
    235                     NullableConverter convertor = new NullableConverter(type);
    236                     return Convert.IsDBNull(value) ? null : convertor.ConvertFrom(value);
    237                 }
    238                 return Convert.ChangeType(Convert.IsDBNull(value) ? null : value, type);
    239             }
    240 
    241             #endregion
    242 
    243             #region 杂项转换
    244             /// <summary>
    245             /// 获取默认值
    246             /// </summary>
    247             /// <param name="type"></param>
    248             /// <returns></returns>
    249             private static object GetNullInternal(Type type)
    250             {
    251                 if (type.IsValueType)
    252                 {
    253                     if (type.IsEnum)
    254                     {
    255                         return GetNullInternal(Enum.GetUnderlyingType(type));
    256                     }
    257                     if (type.IsPrimitive)
    258                     {
    259                         if (type == typeof(int))
    260                         {
    261                             return 0;
    262                         }
    263                         if (type == typeof(double))
    264                         {
    265                             return 0.0;
    266                         }
    267                         if (type == typeof(short))
    268                         {
    269                             return (short)0;
    270                         }
    271                         if (type == typeof(sbyte))
    272                         {
    273                             return (sbyte)0;
    274                         }
    275                         if (type == typeof(long))
    276                         {
    277                             return 0L;
    278                         }
    279                         if (type == typeof(byte))
    280                         {
    281                             return (byte)0;
    282                         }
    283                         if (type == typeof(ushort))
    284                         {
    285                             return (ushort)0;
    286                         }
    287                         if (type == typeof(uint))
    288                         {
    289                             return 0;
    290                         }
    291                         if (type == typeof(ulong))
    292                         {
    293                             return (ulong)0L;
    294                         }
    295                         if (type == typeof(ulong))
    296                         {
    297                             return (ulong)0L;
    298                         }
    299                         if (type == typeof(float))
    300                         {
    301                             return 0f;
    302                         }
    303                         if (type == typeof(bool))
    304                         {
    305                             return false;
    306                         }
    307                         if (type == typeof(char))
    308                         {
    309                             return '\0';
    310                         }
    311                     }
    312                     else
    313                     {
    314                         if (type == typeof(DateTime))
    315                         {
    316                             return DateTime.MinValue;
    317                         }
    318                         if (type == typeof(decimal))
    319                         {
    320                             return 0M;
    321                         }
    322                         if (type == typeof(Guid))
    323                         {
    324                             return Guid.Empty;
    325                         }
    326                         if (type == typeof(TimeSpan))
    327                         {
    328                             return new TimeSpan(0, 0, 0);
    329                         }
    330                     }
    331                 }
    332                 return null;
    333             }
    334             #endregion
    335             #endregion
    336 
    337             #region 转换字符串
    338             /// <summary>
    339             /// 转换字符串,因为可能有空,直接转换会报错
    340             /// </summary>
    341             /// <param name="obj"></param>
    342             /// <param name="defVal"></param>
    343             /// <returns></returns>
    344             public static string ToString(object obj, string defVal = "")
    345             {
    346                 string result = defVal;
    347                 if (obj == null)
    348                 {
    349                     return result;
    350                 }
    351                 return obj.ToString();
    352             }
    353             #endregion
    354         }
    355     }
  • 相关阅读:
    多重背包
    Nginx HTTP负载均衡/反向代理的相关参数测试
    提升磁盘IO性能的几个技巧
    优化HyperV的5个技巧
    Nginx负载均衡
    Linux服务器的优化
    服务器选型事项
    varnish集群
    解决Linux中出现Too many open files
    nginx timeout 配置 全局timeout 局部timeout web timeout
  • 原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/15826246.html
Copyright © 2020-2023  润新知