如何一个TypeCode转换为实际的类型? [英] How to convert a TypeCode to an actual type?

查看:91
本文介绍了如何一个TypeCode转换为实际的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我得到 colType 这是该类型的代码数量。不过,我将如何转换这一数字到实际的类型? THX!



 为(INT J = 0; J< dvColumns.Count; J ++)
{
//获取列的名称。
drvCols = dvColumns [J]。
COLNAME = drvCols.Row.ItemArray [3]的ToString();

//获取列的数据类型代码,并保存它。
colType = Convert.ToInt32(drvCols.Row.ItemArray [11]);
}


解决方案

这是更简单:

 键入类型= Type.GetType(+ colType系统); 

如果您希望将值转换成该类型可以直接使用类型代码



  Convert.ChangeType(值,colType); 


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

这篇关于如何一个TypeCode转换为实际的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆