• C#类型转换2


    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;
            }
        }
    }
  • 相关阅读:
    C#使用Json.Net遍历Json
    晓晨高效IP提取工具 附源码
    JavaScript原生秒表、计时器
    网盘直链工具 winform版 V1.0
    【架构】RPC 使用 Haproxy、keepalive作为负载均衡
    【架构】Google的大规模集群管理工具Borg
    【Networking】gRPC golang 相关资料
    【Linux】find grep 联合使用 过滤所有子目录、文件
    【Storage】Ubuntu LVM 安装配置
    【Ansible】SSH Error: ssh_exchange_identification: Connection closed by remote host
  • 原文地址:https://www.cnblogs.com/hongjiumu/p/2686619.html
Copyright © 2020-2023  润新知