2010-07-21 104 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
​​