开发者

How to convert a TypeCode to an actual type?

In the code below I get colType which is a code number for the type. But how would I conve开发者_运维技巧rt that number into the actual type? Thx!!

for (int j = 0; j < dvColumns.Count; j++)
{
    // Get the name of the column.
    drvCols = dvColumns[j];
    colName = drvCols.Row.ItemArray[3].ToString();

    // Get columns data type code and save it off.
    colType = Convert.ToInt32(drvCols.Row.ItemArray[11]);
}


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);


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;
    }


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);


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));
    }


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜