zoukankan      html  css  js  c++  java
  • c#简单类型转换As<T>

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web;

    namespace Uilt
    {

    //返回类
    public static class ObjectHelper
    {
    //简单类型转换
    public static T As<T>(this object value, T defaultValue = default(T))
    {
    T res = defaultValue;
    if (value == null)//为null返回默认值
    {
    return res;
    }
    object resValue = null;
    Type fromType = value.GetType();
    Type toType = typeof(T);
    TypeCode fromTypeCode = Type.GetTypeCode(fromType);//来源类型
    TypeCode toTypeCode = Type.GetTypeCode(toType);//结果类型
    try
    {
    switch (toTypeCode)
    {

    case TypeCode.Boolean:
    #region bool 转换
    switch (fromTypeCode)
    {
    case TypeCode.SByte:
    resValue = (sbyte)value > 0;
    break;
    case TypeCode.Byte:
    resValue = (byte)value > 0;
    break;
    case TypeCode.Int16:
    resValue = (short)value > 0;
    break;
    case TypeCode.UInt16:
    resValue = (ushort)value > 0;
    break;
    case TypeCode.Int32:
    resValue = (int)value > 0;
    break;
    case TypeCode.UInt32:
    resValue = (uint)value != 0;
    break;
    case TypeCode.Int64:
    resValue = (long)value > 0L;
    break;
    case TypeCode.UInt64:
    resValue = (ulong)value > 0L;
    break;
    case TypeCode.String:
    try
    {
    resValue = int.Parse((string)value) > 0;
    }
    catch
    {
    }
    break;
    }
    resValue = ((IConvertible)value).ToBoolean(null);
    #endregion
    break;
    case TypeCode.Char:
    resValue = ((IConvertible)value).ToChar(null);
    break;
    case TypeCode.SByte:
    resValue = ((IConvertible)value).ToSByte(null);

    break;
    case TypeCode.Byte:
    resValue = ((IConvertible)value).ToByte(null);

    break;
    case TypeCode.Int16:
    resValue = ((IConvertible)value).ToInt16(null);
    break;
    case TypeCode.UInt16:
    resValue = ((IConvertible)value).ToUInt16(null);
    break;
    case TypeCode.Int32:
    resValue = ((IConvertible)value).ToInt32(null);
    break;
    case TypeCode.UInt32:
    resValue = ((IConvertible)value).ToUInt32(null);

    break;
    case TypeCode.Int64:
    resValue = ((IConvertible)value).ToInt64(null);

    break;
    case TypeCode.UInt64:
    resValue = ((IConvertible)value).ToUInt64(null);

    break;
    case TypeCode.Single:
    resValue = ((IConvertible)value).ToSingle(null);

    break;
    case TypeCode.Double:
    resValue = ((IConvertible)value).ToDouble(null);
    break;
    case TypeCode.Decimal:
    resValue = ((IConvertible)value).ToDecimal(null);
    break;
    case TypeCode.DateTime:
    resValue = ((IConvertible)value).ToDateTime(null);
    break;
    case TypeCode.String:
    resValue = (value == null ? string.Empty : ((IConvertible)value).ToString(null));
    break;
    default:
    resValue = value;
    break;
    }


    if (resValue != null)
    {
    res = (T)resValue;
    }
    }
    catch (Exception)
    {

    }
    return res;
    }
    }
    }

  • 相关阅读:
    编译安装LAMP之安装Apache+php与管理(十五)
    编译安装Mysql与管理(十四)
    linux服务器的性能分析与优化(十三)
    awk和sed (十二)
    Bash Shell (十一)
    linux 计划任务(十)
    Linux文本编辑器(九)
    linux rsync介绍(八)
    yum安装LAMP环境与管理(七)
    mysql date数据类型异常原因0000-00
  • 原文地址:https://www.cnblogs.com/Wilson6/p/12147292.html
Copyright © 2011-2022 走看看