我有一個非常奇怪的問題,在Visual Studio 2010調試器中針對.NET 4.0的C#控制檯應用程序的局部變量超出上下文。我已經在SO上搜索了其他類似的問題,但是雖然有些症狀相同,但似乎沒有直接適用於此問題(它們似乎都有其他根本原因)。Out of Context變量在Visual Studio 2010調試器
問題是,對於一些變量(但不是全部)我沒有得到一個工具提示與他們的價值,他們不出現在當地人窗口,我得到「姓名'xyz'不存在當前上下文「,如果我將它們添加到監視窗口。它似乎會影響一些變量,但不會影響其他變量,我無法弄清楚一個模式(它似乎不是基於成員與本地,類與結構或其他區分標記)。我重新啓動了我的電腦和Visual Studio,驗證了我在一個乾淨的Debug版本中,確保調試框架是正確的,確保刷新了手表屏幕中的變量,並嘗試了各種法術和咒語。
我已經在下面添加了一個屏幕截圖(更大的版本在http://i.stack.imgur.com/JTFBT.png)。
有什麼想法?
編輯:
添加一些額外的信息:
的問題是重複的。即使在完全關閉並重新啓動Visual Studio之後,完全相同的變量也可以工作或無法工作。這使我相信,實際上有些系統性的錯誤,而不僅僅是記憶腐敗或什麼。
我也發現它似乎與try-catch塊有關。如果我在try語句之外定位斷點,我通常可以看到任何範圍內的變量。只要執行點進入try語句,try塊外的所有變量都變得不可訪問,我只能訪問try語句內的那些變量。這幾乎就像調試器將try塊視爲一個單獨的方法(儘管您可以看到代碼/編譯器仍然可以訪問範圍內的變量)。有沒有人見過這種行爲?
ANOTHER編輯:
我(部分)收回我剛纔說在try-catch是犯罪嫌疑人 - 似乎在任何封閉的代碼調試展品這個奇怪的回吐東西的這部分斷章取義塊。例如,如果我在截圖的foreach語句中直接設置斷點,我可以在每次迭代中看到「端口」變量值,但foreach語句之外的變量都沒有(只要我進入foreach塊就會消失) 。然後一旦你進入try塊,「port」變量突然消失。這變得非常奇怪。
此外,根據要求,整個方法的代碼如下。
private void ConfigureAnnouncerSockets(XDocument configDocument)
{
XElement socketsElement = configDocument.XPathSelectElement("/Configuration/Network/AnnouncerSockets");
bool useDefault = true;
if (socketsElement != null)
{
//Use the default announcers? (they will be added at the end)
XAttribute defaultAttribute = socketsElement.Attribute("useDefault");
if (defaultAttribute != null)
{
useDefault = Convert.ToBoolean(defaultAttribute);
}
//Get the default frequency
int defaultFrequency = Announcer.DefaultFrequency;
XAttribute frequencyAttribute = socketsElement.Attribute("frequency");
if (frequencyAttribute != null)
{
defaultFrequency = Convert.ToInt32(frequencyAttribute.Value);
}
//Get all sockets
foreach (XElement socketElement in socketsElement.XPathSelectElements("./Socket"))
{
//Get the address
IPAddress address = IPAddress.Broadcast;
string addressAttribute = (string)socketElement.Attribute("address");
if(!GetAddress(addressAttribute, ref address, true))
{
Intelliplex.Log.Warn("Invalid announcer socket address: " + addressAttribute);
continue;
}
//Get the local address
IPAddress localAddress = null;
string localAddressAttribute = (string)socketElement.Attribute("localAddress");
if(!GetAddress(localAddressAttribute, ref localAddress, false))
{
Intelliplex.Log.Warn("Invalid announcer socket local address: " + localAddressAttribute);
continue;
}
//Get the port(s)
List<int> ports = new List<int>();
string[] ranges = ((string)socketElement.Attribute("port")).Split(new[] { ',' });
foreach (string range in ranges)
{
string[] portPair = range.Split(new[] { '-' });
int firstPort = Convert.ToInt32(portPair[0]);
int lastPort = portPair.Length > 1 ? Convert.ToInt32(portPair[1]) : firstPort;
do
{
ports.Add(firstPort);
} while (++firstPort <= lastPort);
}
//Get the local port
int localPort = socketElement.Attribute("localPort") != null
? Convert.ToInt32((string)socketElement.Attribute("localPort")) : 0;
//Get the frequency
int frequency = socketElement.Attribute("frequency") != null
? Convert.ToInt32((string)socketElement.Attribute("frequency")) : defaultFrequency;
//Create the socket(s) and add it/them to the manager
foreach (int port in ports)
{
try
{
IPEndPoint endPoint = new IPEndPoint(address, port);
IPEndPoint localEndPoint = localAddress == null
? new IPEndPoint(IPAddress.Any, 0) : new IPEndPoint(localAddress, localPort);
Announcer socket = new Announcer(frequency, endPoint, localEndPoint);
AnnouncerSockets.Add(socket);
}
catch (Exception ex)
{
Intelliplex.Log.Warn("Could not add announcer socket: " + ex.Message);
}
}
}
}
//Add default announcement sockets?
if (useDefault)
{
ConfigureDefaultAnnouncerSockets();
}
}
+1爲自由圈圈 – James 2011-12-29 19:44:03
檢查您的項目屬性:您確定您的調試配置沒有被更改爲「優化代碼」? – phoog 2011-12-29 19:57:23
@phoog感謝您的建議。我剛剛檢查了項目屬性,並且「優化代碼」未被選中。 – daveaglick 2011-12-29 20:01:13