位置:海鸟网 > IT > ASP.NET >

.net 数据类型转换类(带判断)

using System;
using System.Collections.Generic;
using System.Text;

namespace Devin.Common.Helper
{
    ///
    /// 数据类型转换
    ///
    public class DataConvert
    {
        ///
        /// 转成String类型
        ///
        ///
        ///
        public static String GetString(Object V)
        {
            return GetString(V, String.Empty);

        }
        ///
        /// 转成String类型
        ///
        ///
        ///
        ///
        public static String GetString(Object V, String defaultValue)
        {
            if (!IsNull(V))
            {
                return V.ToString().Trim().Replace(">", ">").Replace("<", "<").Replace("'", "'");
            }
            return defaultValue;
        }
        ///
        /// 转成String类型
        ///
        ///
        ///
        public static String SetString(Object V)
        {
            if (!IsNull(V))
            {
                return V.ToString().Trim().Replace(">", ">").Replace("<", "<").Replace("'", "'");
            }
            return "";
        }
        ///
        /// 转成Guid类型
        ///
        ///
        ///
        public static Guid GetGuid(Object V)
        {
            return GetGuid(V, Guid.Empty);
        }
        ///
        /// 转成Guid类型
        ///
        ///
        ///
        ///
        public static Guid GetGuid(Object V, Guid defaultValue)
        {
            if (!(!IsNull(V) && IsGuid(V)))
            {
                return defaultValue;
            }
            return new Guid(V.ToString());
        }
        ///
        /// 转成Int32类型
        ///
        ///
        ///
        public static Int32 GetInt32(Object V)
        {
            return GetInt32(V, 0);
        }
        ///
        /// 转成Int32类型
        ///
        ///
        ///
        ///
        public static Int32 GetInt32(Object V, Int32 defaultValue)
        {
            if (!(!IsNull(V) && IsInt32(V)))
            {
                return defaultValue;
            }
            return Convert.ToInt32(V);
        }
        ///
        /// 转成Int16类型
        ///
        ///
        ///
        public static Int16 GetInt16(Object V)
        {
            return GetInt16(V, 0);
        }
        ///
        /// 转成Int16类型
        ///
        ///
        ///
        ///
        public static Int16 GetInt16(Object V, Int16 defaultValue)
        {
            if (!(!IsNull(V) && IsInt16(V)))
            {
                return defaultValue;
            }
            return Convert.ToInt16(V);
        }
        ///
        /// 转成Int64类型
        ///
        ///
        ///
        public static Int64 GetInt64(Object V)
        {
            return GetInt64(V, 0);
        }
        ///
        /// 转成Int64类型
        ///
        ///
        ///
        ///
        public static Int64 GetInt64(Object V, Int64 defaultValue)
        {
            if (!(!IsNull(V) && IsInt64(V)))
            {
                return defaultValue;
            }
            return Convert.ToInt64(V);
        }
        ///
        /// 转成Double类型
        ///
        ///
        ///
        public static Double GetDouble(Object V)
        {
            return GetDouble(V, 0);
        }
        ///
        /// 转成Double类型
        ///
        ///
        ///
        ///
        public static Double GetDouble(Object V, Double defaultValue)
        {
            if (!(!IsNull(V) && IsDouble(V)))
            {
                return defaultValue;
            }
            return Convert.ToDouble(V);
        }
        ///
        /// 转成Boolean类型
        ///
        ///
        ///
        public static Boolean GetBoolean(Object V)
        {
            return GetBoolean(V, false);
        }
        ///
        /// 转成Boolean类型
        ///
        ///
        ///
        ///
        public static Boolean GetBoolean(Object V, Boolean defaultValue)
        {
            if (!(!IsNull(V) && IsBoolean(V)))
            {
                return defaultValue;
            }
            return Convert.ToBoolean(V);
        }
        ///
        /// 转成DateTime类型
        ///
        ///
        ///
        public static DateTime GetDateTime(Object V)
        {
            return GetDateTime(V, DateTime.Now);
        }
        ///
        /// 转成DateTime类型
        ///
        ///
        ///
        ///
        public static DateTime GetDateTime(Object V, DateTime defaultValue)
        {
            if (!(!IsNull(V) && IsDateTime(V)))
            {
                return defaultValue;
            }
            return Convert.ToDateTime(V);
        }
        ///
        /// 转成Decimal类型
        ///
        ///
        ///
        public static Decimal GetDecimal(Object V)
        {
            return GetDecimal(V, 0);
        }
        ///
        /// 转成Decimal类型
        ///
        ///
        ///
        ///
        public static Decimal GetDecimal(Object V, Decimal defaultValue)
        {
            if (!(!IsNull(V) && IsDecimal(V)))
            {
                return defaultValue;
            }
            return Convert.ToDecimal(V);
        }
        ///
        /// 转成Single类型
        ///
        ///
        ///
        public static Single GetSingle(Object V)
        {
            return GetSingle(V, 0);
        }
        ///
        /// 转成Single类型
        ///
        ///
        ///
        ///
        public static Single GetSingle(Object V, Single defaultValue)
        {
            if (!(!IsNull(V) && IsSingle(V)))
            {
                return defaultValue;
            }
            return Convert.ToSingle(V);
        }
        ///
        /// 是否为空
        ///
        ///
        ///
        public static Boolean IsNull(Object V)
        {
            if (!((V == null) || Convert.IsDBNull(V)))
            {
                return false;
            }
            return true;
        }
        ///
        /// 检查是否为 int 型
        ///
        ///
        ///
        public static Boolean IsNumeric(String v)
        {
            try
            {
                int var1 = Convert.ToInt32(v);
                return true;
            }
            catch
            {
                return false;
            }
        }
        ///
        /// 是否为 Guid 类型
        ///
        ///
        ///
        public static Boolean IsGuid(Object V)
        {
            try
            {
                Guid guid = new Guid(V.ToString());
                return true;
            }
            catch
            {
                return false;
            }
        }
        ///
        /// 是否为Int32类型
        ///
        ///
        ///
        public static Boolean IsInt32(Object V)
        {
            try
            {
                Convert.ToInt32(V);
                return true;
            }
            catch
            {
                return false;
            }
        }
        ///
        /// 是否为Int16类型
        ///
        ///
        ///
        public static Boolean IsInt16(Object V)
        {
            try
            {
                Convert.ToInt16(V);
                return true;
            }
            catch
            {
                return false;
            }
        }
        ///
        /// 是否为Int64类型
        ///
        ///
        ///
        public static Boolean IsInt64(Object V)
        {
            try
            {
                Convert.ToInt64(V);
                return true;
            }
            catch
            {
                return false;
            }
        }
        ///
        /// 是否为Double类型
        ///
        ///
        ///
        public static Boolean IsDouble(Object V)
        {
            try
            {
                Convert.ToDouble(V);
                return true;
            }
            catch
            {
                return false;
            }
        }
        ///
        /// 是否为DateTime类型
        ///
        ///
        ///
        public static Boolean IsDateTime(Object V)
        {
            try
            {
                Convert.ToDateTime(V);
                return true;
            }
            catch
            {
                return false;
            }
        }
        ///
        /// 是否为Decimal类型
        ///
        ///
        ///
        public static Boolean IsDecimal(Object V)
        {
            try
            {
                Convert.ToDecimal(V);
                return true;
            }
            catch
            {
                return false;
            }
        }
        ///
        /// 是否为Boolean类型
        ///
        ///
        ///
        public static Boolean IsBoolean(Object V)
        {
            try
            {
                Convert.ToBoolean(V);
                return true;
            }
            catch
            {
                return false;
            }
        }
        ///
        /// 是否为Single类型
        ///
        ///
        ///
        public static Boolean IsSingle(Object V)
        {
            try
            {
                Convert.ToSingle(V);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}