zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    1.1 What is the plug-in?
    Chapter 8 The Simplest Plug-in Solution
    Chapter 7 Resources in Plug-In(1)
    Chapter 1: Plug-in programing from past to the future
    Android插件化的兼容性(下):突破Android P中灰黑名单的限制
    Android插件化的兼容性(中):Android P的适配
    Android插件化的兼容性(上):Android O的适配
    pandas 学习 第12篇:DataFrame 避免链式赋值
    NumPy 学习 第三篇:矢量化和广播
    Wait Type 整理
  • 原文地址:https://www.cnblogs.com/hongjiumu/p/2686619.html
Copyright © 2011-2022 走看看