1
我有我從中讀出的單元測試數據的XML文件:[DataSource]
的XML行:<DataBool>true</DataBool>
,我嘗試讀取值:的DataRow爲bool鑄造
bool bData = (bool)TestContext.DataRow[4];
但它拋出異常 - 無效鑄造。可能的有效解決方案是什麼?
我有我從中讀出的單元測試數據的XML文件:[DataSource]
的XML行:<DataBool>true</DataBool>
,我嘗試讀取值:的DataRow爲bool鑄造
bool bData = (bool)TestContext.DataRow[4];
但它拋出異常 - 無效鑄造。可能的有效解決方案是什麼?
如果沒有與之相關的模式,那麼它可能只是一個字符串。如果是這樣,請使用bool.Parse
或bool.TryParse
將其從字符串轉換爲布爾值。
TestContext.DataRow [4]'輸出什麼? – balexandre
它可能是'bool?'而不是'bool'類型(問號不是拼寫錯誤),因爲源代碼中的類型實際上可能是['Nullable''(http://msdn.microsoft.com/ en-us/library/1t3y8s4s.aspx) –
Sam
TestContext.DataRow [4] .GetType()。ToString()是什麼? – Sam