2011-05-06 63 views
13

在下面的代码我得到colType这是类型的代码数。但是,我将如何将该数字转换为实际类型?谢谢!!如何将TypeCode转换为实际类型?

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]); 
} 
+4

给所述输入的一个例子。 – Tejs 2011-05-06 18:50:53

+1

您必须使用colType值的开关大小写。 – 2011-05-06 19:19:55

+0

问题是我不知道什么类型的数字代表什么类型。例如,返回的其中一个数字是72.我怎么知道72型应该代表什么? – user259286 2011-05-06 19:22:53

回答

3

我不认为有什么办法可以在.NET Framework本身做到这一点,因为我已经看到了使用一个大的switch语句来处理转换(例如:here)的所有例子。

但是,如果您尝试将类型作为将对象转换为该类型的中间步骤,则始终可以使用接受TypeCode作为参数的Convert.ChangeType

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

不幸的是,没有看到你正在尝试做的我真的不能说,如果ChangeType将是有益与否。

编辑:

要将int转换为OleDbType,你可以将它转换:

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

如果有'Convert.TryChangeType (d,TypeCode.Int32,出牛逼V)'只是因为有些情况下,你可能会使用'开关(类型码)实例'办理值类型这将是很好。否则,你最终执行上'D'各种检查,以确保'Convert'不抛出异常。 – IAbstract 2016-04-28 18:49:57

11

使用switch语句:

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

这是更简单:

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

如果您希望将值转换为这种类型的,你可以直接使用

Convert.ChangeType(value, colType); 
+0

看到一个衬垫确切回答,我写了下面使用schoetbi的提示和'Enum.GetName(typeof运算(类型码)'在 http://stackoverflow.com/a/30780954/1300390 – 2016-05-16 20:45:34

3

确切的答案,在OP的问题(使用schoetbi的提示)类型代码为:

public static Type GetType(TypeCode code) 
    {   
     return Type.GetType("System." + Enum.GetName(typeof(TypeCode), code)); 
    } 
+0

我测试过它和问题它适用于我,没有任何问题 – 2016-01-08 17:29:04

2

以下是我喜欢的方式。我更喜欢使用一个静态表与一个大开关或反射。

/// <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

'switch'是一个实现细节,很容易隐藏在一个扩展方法中,'switch'由于汇编而不是通过映射进行运行时解码,所以比字典更快。 – IAbstract 2016-04-28 18:54:38

+1

很久以前,当我深入C++时,Scott Meyers告诉我switch语句通常运行O(n),而我们往往可以做得更好,所以我倾向于使用maps/dictionaries /哈希表不仅可读性,但通常O(1)的表现。我想资料这个,看看什么是真正的C#重要的。 – BrandonLWhite 2016-04-28 19:34:13

+2

那么事实证明他们是comparab乐。在Release版本中,交换机在字典上有一个非常轻微的优势。在Debug版本中(如果有人关心),Dictionary实际上比较了很多。很有意思。我的结论是,编译器优化开关的行为更像哈希表。所以在性能方面,这是一种洗涤。做你认为更可读的东西。 https://gist.github.com/BrandonLWhite/d823dcbddc256be6fd879b7be7d3960f – BrandonLWhite 2016-04-28 21:15:07

相关问题