How to convert a TypeCode to an actual type?

后端 未结 5 1703
一向
一向 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:42

    It's even simpler:

    Type type = Type.GetType("System." + colType);
    

    If you want to convert a value to that type you can use the typecode directly

    Convert.ChangeType(value, colType);
    
    0 讨论(0)
  • 2021-02-19 03:50

    I don't think there is any way to do this natively in the .NET Framework, as all the examples I've seen use a big switch statement to handle the conversion (for example: here).

    However, if you are trying to get the type as an intermediary step towards converting an object to that type, you could always use Convert.ChangeType which accepts a TypeCode as a parameter.

    double d = -1.234;
    int i = (int)Convert.ChangeType(d, TypeCode.Int32);
    

    Unfortunately, without seeing what you are trying to do I can't really say if ChangeType would be helpful or not.

    EDIT:

    To convert an int to a OleDbType, you can just cast it:

    int i = 72; //72 is the value for OleDbType.Guid
    if(Enum.IsDefined(typeof(System.Data.OleDb.OleDbType), i))
    {
        System.Data.OleDb.OleDbType dbType = (System.Data.OleDb.OleDbType)i;
        Console.WriteLine(dbType);
    }
    else
        Console.WriteLine("{0} is not defined for System.Data.OleDb.OleDbType", i);
    
    0 讨论(0)
  • 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;
        }
    
    0 讨论(0)
  • 2021-02-19 03:53

    Here's how I like to do it. I prefer using a static table vs. a big switch or reflection for this.

    /// <summary>
    /// Table that maps TypeCode to it's corresponding Type.
    /// </summary>
    static IReadOnlyDictionary<TypeCode, Type> TypeCodeToTypeMap = new Dictionary<TypeCode, Type>
    {
        { TypeCode.Boolean, typeof(bool) },
        { TypeCode.Byte, typeof(byte) },
        { TypeCode.Char, typeof(char) },
        { TypeCode.DateTime, typeof(DateTime) },
        { TypeCode.DBNull, typeof(DBNull) },
        { TypeCode.Decimal, typeof(decimal) },
        { TypeCode.Double, typeof(double) },
        { TypeCode.Empty, null },
        { TypeCode.Int16, typeof(short) },
        { TypeCode.Int32, typeof(int) },
        { TypeCode.Int64, typeof(long) },
        { TypeCode.Object, typeof(object) },
        { TypeCode.SByte, typeof(sbyte) },
        { TypeCode.Single, typeof(Single) },
        { TypeCode.String, typeof(string) },
        { TypeCode.UInt16, typeof(UInt16) },
        { TypeCode.UInt32, typeof(UInt32) },
        { TypeCode.UInt64, typeof(UInt64) }
    };
    
    /// <summary>
    /// Convert a TypeCode ordinal into it's corresponding Type instance.
    /// </summary>
    public static Type ToType(this TypeCode code)
    {
        Type type = null;
    
        TypeCodeToTypeMap.TryGetValue(code, out type);
    
        return type;
    }
    
    0 讨论(0)
  • 2021-02-19 04:02

    The exact answer to the question in the OP (using schoetbi's hint) would be:

    public static Type GetType(TypeCode code)
        {         
            return Type.GetType("System." + Enum.GetName(typeof(TypeCode), code));
        }
    
    0 讨论(0)
提交回复
热议问题