C#类型转换3

本文介绍了一组用于C#的类型扩展方法,包括检查原始类型、字符串类型、布尔类型等,同时还提供了获取默认值、连接字符串数组、生成MD5等功能的方法实现。
namespace WDBuyNET.DMSFrame.Utils
{
    public static class TypeExtentions
    {
        public static bool IsPrimitive(this Type t)
        {
            bool result;
            if (t.IsGenericType)
            {
                result = (TypeExtentions.IsNullableType(t) && TypeExtentions.IsPrimitive(Nullable.GetUnderlyingType(t)));
            }
            else
            {
                bool arg_14F_0;
                if (!(t == typeof(string)) && !(t == typeof(short)) && !(t == typeof(ushort)) && !(t == typeof(int)) && !(t == typeof(uint)) && !(t == typeof(long)) && !(t == typeof(ulong)) && !(t == typeof(float)) && !(t == typeof(double)) && !(t == typeof(decimal)) && !(t == typeof(char)) && !(t == typeof(byte)) && !(t == typeof(bool)) && !(t == typeof(DateTime)))
                {
                    arg_14F_0 = (t == typeof(Guid));
                }
                else
                {
                    arg_14F_0 = true;
                }
                result = arg_14F_0;
            }
            return result;
        }
        public static bool IsStringType(this Type type)
        {
            bool arg_80_0;
            if (!(type == typeof(string)) && !(type == typeof(bool)) && !(type == typeof(DateTime)) && !(type == typeof(Guid)) && !(type == typeof(bool?)) && !(type == typeof(DateTime?)))
            {
                arg_80_0 = (type == typeof(Guid?));
            }
            else
            {
                arg_80_0 = true;
            }
            return arg_80_0;
        }
        public static bool IsBooleanType(this Type type)
        {
            return TypeExtentions.GetUnderlyingType(type) == typeof(bool);
        }
        public static bool AllowsNullValue(this Type type)
        {
            return !type.IsValueType || TypeExtentions.IsNullableType(type);
        }
        public static bool IsNullableType(this Type type)
        {
            return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable);
        }
        public static Type GetUnderlyingType(this Type type)
        {
            return TypeExtentions.IsNullableType(type) ? Nullable.GetUnderlyingType(type) : type;
        }
        public static string Join(this string[] array, char splitChar)
        {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < array.Length; i++)
            {
                stringBuilder.Append(array[i]);
                stringBuilder.Append(splitChar);
            }
            return stringBuilder.ToString().TrimEnd(new char[]
            {
                splitChar
            });
        }
        public static object DefaultValue(this Type type)
        {
            return type.IsValueType ? Activator.CreateInstance(type) : null;
        }
        public static string MD5(this string text)
        {
            byte[] bytes = Encoding.Default.GetBytes(text);
            MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
            byte[] array = mD5CryptoServiceProvider.ComputeHash(bytes);
            string text2 = "";
            byte[] array2 = array;
            for (int i = 0; i < array2.Length; i++)
            {
                byte b = array2[i];
                text2 = ((b >= 16) ? (text2 + b.ToString("X")) : (text2 + "0" + b.ToString("X")));
            }
            return text2.ToLower();
        }
    }
}

转载于:https://www.cnblogs.com/hongjiumu/archive/2012/09/15/2686622.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值