How to convert a TypeCode to an actual type?

后端 未结 5 1706
一向
一向 2021-02-19 03:14

In the code below I get colType which is a code number for the type. But how would I convert that number into the actual type? Thx!!

for (int j = 0;         


        
5条回答
  •  無奈伤痛
    2021-02-19 03:52

    Using a switch statement:

        public static Type ToType(this TypeCode code)
        {
            switch (code)
            {
                case TypeCode.Boolean:
                    return typeof(bool);
    
                case TypeCode.Byte:
                    return typeof(byte);
    
                case TypeCode.Char:
                    return typeof(char);
    
                case TypeCode.DateTime:
                    return typeof(DateTime);
    
                case TypeCode.DBNull:
                    return typeof(DBNull);
    
                case TypeCode.Decimal:
                    return typeof(decimal);
    
                case TypeCode.Double:
                    return typeof(double);
    
                case TypeCode.Empty:
                    return null;
    
                case TypeCode.Int16:
                    return typeof(short);
    
                case TypeCode.Int32:
                    return typeof(int);
    
                case TypeCode.Int64:
                    return typeof(long);
    
                case TypeCode.Object:
                    return typeof(object);
    
                case TypeCode.SByte:
                    return typeof(sbyte);
    
                case TypeCode.Single:
                    return typeof(Single);
    
                case TypeCode.String:
                    return typeof(string);
    
                case TypeCode.UInt16:
                    return typeof(UInt16);
    
                case TypeCode.UInt32:
                    return typeof(UInt32);
    
                case TypeCode.UInt64:
                    return typeof(UInt64);
            }
    
            return null;
        }
    

提交回复
热议问题