2010-07-21 64 views
4

DataSet.Tables[0].Columns[0]我们有一个DataType属性。现在,我想遍历Columns并根据DataType中的Type执行一些操作。这个怎么做?如何比较System.Type?

foreach(var c in DataSet.Tables[0].Columns) 
{ 
    if (c.DataType == System.String) {} //error 'string' is a 'type', which is not valid in the given context 

} 

回答

12

使用typeof操作:

if (c.DataType == typeof(string)) 
{ 
    // ... 
} 
+0

或'(c.DataType是字符串)'。 – Marc 2010-07-21 13:56:02

+5

@马克:这不是一回事。 'c.DataType'是'Type',而不是'string'。您的代码将始终评估为false。 – LukeH 2010-07-21 14:00:33

+0

我想知道我错过了什么,谢谢!我发表了这个评论,希望它会受到指责或证实。 – Marc 2010-07-21 14:02:17

4

尝试......

if (c.DataType == typeof(string)) {}
4
​​