建站学 - 轻松建站从此开始!

建站学-个人建站指南,网页制作,网站设计,网站制作教程

当前位置: 建站学 > 网站开发 > asp.net教程 >

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

时间:2011-04-24 10:18来源: 作者: 点击:
using System; using System.Collections.Generic; using System.Text; namespace Devin.Common.Helper {     ///     /// 数据类型转换     ///     public class DataConvert     {  

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(">", "&gt;").Replace("<", "&lt;").Replace("'", "&#39;");
            }
            return defaultValue;
        }
        ///
        /// 转成String类型
        ///
        ///
        ///
        public static String SetString(Object V)
        {
            if (!IsNull(V))
            {
                return V.ToString().Trim().Replace("&gt;", ">").Replace("&lt;", "<").Replace("&#39;", "'");
            }
            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;
            }
        }
    }
}

(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片