我想讀我編譯的C#代碼。C#,奇怪的優化
這是我的代碼:
using(OleDbCommand insertCommand = new OleDbCommand("...", connection))
{
// do super stuff
}
但是!
我們都知道,使用被轉換到這一點:
{
OleDbCommand insertCommand = new OleDbCommand("...", connection)
try
{
//do super stuff
}
finally
{
if(insertCommand != null)
((IDisposable)insertCommand).Dispose();
}
}
(因爲OleDbCommand的是引用類型)。
但是當我反編譯我的組件(使用.NET 2.0編譯)我得到這個ReSharper的:
try
{
insertCommand = new OleDbCommand("", connection);
Label_0017:
try
{
//do super stuff
}
finally
{
Label_0111:
if ((insertCommand == null) != null)
{
goto Label_0122;
}
insertCommand.Dispose();
Label_0122:;
}
我說的這條線:if ((insertCommand == null) != null)
。
假設insertCommand爲null。然後第一部分返回true。 (true != null)
返回true
。那麼處置仍然被忽略?奇怪,非常奇怪。
如果我粘貼在Visual Studio中,ReSharper的已經警告我:表情總是真的...
謝謝!
-Kristof
我誤讀了這個問題,所以刪掉了我的答案。在正確地閱讀你的問題後,我猜想Resharper中存在一個錯誤,我建議嘗試其他反編譯器,看看你得到了什麼結果。 – 2010-05-07 13:21:03
你用什麼工具反編譯?也許你應該試試另一個 – 2010-05-07 13:22:06