問題是:我運行此代碼(代碼1),并在瀏覽器(ASP.NET)中獲得System.InvalidCastException。完全相同的代碼(在我的例子中,我使用另一個項目中的這個函數),在另一個項目中運行并使用相同的數據工作正常,并且不會拋出任何異常。我嘗試捕獲此異常并查看問題對象(代碼 2)的內部,但隨后令我驚訝的是沒有捕獲異常(我在 catch 代碼塊的第二行放置了一個斷點以確保)并且得到了正確的輸出在瀏覽器中代碼1private static List<Dictionary<string, object>> GetResultRows(SQLiteDataReader Reader) { List<Dictionary<string, object>> Result = new List<Dictionary<string, object>>(); if (!Reader.HasRows) return Result; while (Reader.Read()) { var CurrentRow = new Dictionary<string, object>(); Result.Add(CurrentRow); for (int i = 0; i < Reader.FieldCount; i++) { CurrentRow.Add(Reader.GetName(i), Reader.GetString(i)); } } return Result; }代碼2private static List<Dictionary<string, object>> GetResultRows(SQLiteDataReader Reader) { List<Dictionary<string, object>> Result = new List<Dictionary<string, object>>(); if (!Reader.HasRows) return Result; while (Reader.Read()) { var CurrentRow = new Dictionary<string, object>(); Result.Add(CurrentRow); for (int i = 0; i < Reader.FieldCount; i++) { //CurrentRow.Add(Reader.GetName(i), Reader.GetString(i)); try { CurrentRow.Add(Reader.GetName(i), Reader.GetString(i)); } catch { var temp = Reader.GetValue(i); System.Diagnostics.Debug.WriteLine($"{Reader.GetName(i)} = {Reader.GetValue(i)}"); } } } return Result; }我希望這個函數盡可能快,所以 try-catch 無法解決我的問題。有任何想法嗎?UPD:瀏覽器異常:圖像
- 2 回答
- 0 關注
- 189 瀏覽
添加回答
舉報
0/150
提交
取消