代码改变世界

C#类型转换2

2012-09-15 16:55  hongjiumu  阅读(322)  评论(0)    收藏  举报
namespace WDBuyNET.DMSFrame.Utils.Helpers
{
    public static class TypeHelper
    {
        public static object ChangeType(Type targetType, object val)
        {
            object result;
            if (val == null)
            {
                result = null;
            }
            else
            {
                if (targetType.IsAssignableFrom(val.GetType()))
                {
                    result = val;
                }
                else
                {
                    if (targetType == val.GetType())
                    {
                        result = val;
                    }
                    else
                    {
                        if (targetType == typeof(bool))
                        {
                            if (val.ToString() == "0")
                            {
                                result = false;
                                return result;
                            }
                            if (val.ToString() == "1")
                            {
                                result = true;
                                return result;
                            }
                        }
                        if (targetType.IsEnum)
                        {
                            int num = 0;
                            result = (int.TryParse(val.ToString(), out num) ? val : Enum.Parse(targetType, val.ToString()));
                        }
                        else
                        {
                            result = ((!(targetType == typeof(Type))) ? ((!(targetType == typeof(IComparable))) ? Convert.ChangeType(val, targetType) : val) : ReflectionHelper.GetType(val.ToString()));
                        }
                    }
                }
            }
            return result;
        }
        public static string GetClassSimpleName(Type t)
        {
            string[] array = t.ToString().Split(new char[]
            {
                '.'
            });
            return array[array.Length - 1].ToString();
        }
        public static object GetDefaultValue(Type destType)
        {
            return (!TypeHelper.IsNumbericType(destType)) ? ((!(destType == typeof(string))) ? ((!(destType == typeof(bool))) ? ((!(destType == typeof(DateTime))) ? ((!(destType == typeof(Guid))) ? ((!(destType == typeof(TimeSpan))) ? null : TimeSpan.Zero) : Guid.NewGuid()) : DateTime.Now) : false) : "") : 0;
        }
        public static string GetDefaultValueString(Type destType)
        {
            return (!TypeHelper.IsNumbericType(destType)) ? ((!(destType == typeof(string))) ? ((!(destType == typeof(bool))) ? ((!(destType == typeof(DateTime))) ? ((!(destType == typeof(Guid))) ? ((!(destType == typeof(TimeSpan))) ? "null" : "System.TimeSpan.Zero") : "System.Guid.NewGuid()") : "DateTime.Now") : "false") : "\"\"") : "0";
        }
        public static Type GetTypeByRegularName(string regularName)
        {
            return ReflectionHelper.GetType(regularName);
        }
        public static string GetTypeRegularName(Type destType)
        {
            string arg1 = destType.Assembly.FullName.Split(new char[]
            {
                ','
            })[0];
            return string.Format("{0},{1}", destType.ToString(), arg1);
        }
        public static string GetTypeRegularNameOf(object obj)
        {
            return TypeHelper.GetTypeRegularName(obj.GetType());
        }
        public static bool IsFixLength(Type destDataType)
        {
            bool arg_46_0;
            if (!TypeHelper.IsNumbericType(destDataType))
            {
                if (!(destDataType == typeof(byte[])))
                {
                    arg_46_0 = (destDataType == typeof(DateTime) || destDataType == typeof(bool));
                }
                else
                {
                    arg_46_0 = true;
                }
            }
            else
            {
                arg_46_0 = true;
            }
            return arg_46_0;
        }
        public static bool IsIntegerCompatibleType(Type destDataType)
        {
            bool arg_92_0;
            if (!(destDataType == typeof(int)) && !(destDataType == typeof(uint)) && !(destDataType == typeof(short)) && !(destDataType == typeof(ushort)) && !(destDataType == typeof(long)) && !(destDataType == typeof(ulong)) && !(destDataType == typeof(byte)))
            {
                arg_92_0 = (destDataType == typeof(sbyte));
            }
            else
            {
                arg_92_0 = true;
            }
            return arg_92_0;
        }
        public static bool IsNumbericType(Type destDataType)
        {
            bool arg_D1_0;
            if (!(destDataType == typeof(int)) && !(destDataType == typeof(uint)) && !(destDataType == typeof(double)) && !(destDataType == typeof(short)) && !(destDataType == typeof(ushort)) && !(destDataType == typeof(decimal)) && !(destDataType == typeof(long)) && !(destDataType == typeof(ulong)) && !(destDataType == typeof(float)) && !(destDataType == typeof(byte)))
            {
                arg_D1_0 = (destDataType == typeof(sbyte));
            }
            else
            {
                arg_D1_0 = true;
            }
            return arg_D1_0;
        }
        public static bool IsSimpleType(Type t)
        {
            bool arg_7B_0;
            if (!TypeHelper.IsNumbericType(t))
            {
                if (!(t == typeof(char)))
                {
                    if (!(t == typeof(string)))
                    {
                        if (!(t == typeof(bool)))
                        {
                            if (!(t == typeof(DateTime)))
                            {
                                arg_7B_0 = (t == typeof(Type) || t.IsEnum);
                            }
                            else
                            {
                                arg_7B_0 = true;
                            }
                        }
                        else
                        {
                            arg_7B_0 = true;
                        }
                    }
                    else
                    {
                        arg_7B_0 = true;
                    }
                }
                else
                {
                    arg_7B_0 = true;
                }
            }
            else
            {
                arg_7B_0 = true;
            }
            return arg_7B_0;
        }
    }
}