为什么我会得到一个异常InvalidOperationException
本文关键字:一个 异常 InvalidOperationException 为什么 | 更新日期: 2023-09-27 18:01:08
我已经编写了以下代码,它从listBox中删除了选定的值。我还想将它从字典列表中删除,然后更新/写入文本文件,这样当我再次运行程序并加载文本文件时,它会被更新,如果没有,每次我再次运行应用程序时,它都会继续显示删除的项目。
private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
string sb;
if (e.KeyCode == Keys.Delete)
{
if (this.listBox1.SelectedIndex >= 0)
{
string obj = this.listBox1.SelectedValue.ToString();
data.Remove(obj);
listBox1.DataSource = null;
listBox1.DataSource = data;
foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
{
for (int i = 0; i < kvp.Value.Count(); i++)
{
sb = "Url: " + kvp.Key + " --- " + "Local KeyWord: " + kvp.Value[i] + Environment.NewLine;
LocalyKeyWords.Remove(kvp.Key);
}
}
}
}
}
LocalyKeyWords是一个字典>
在这种情况下,它包含两个项/键,我删除了其中一个,并通过断点看到这一个已被删除。
问题是我是否需要删除kvp。键或以某种方式删除我从列表框中删除的项目,它是我想从LocalyKeywords中删除的,它是obj变量,因为我正在做:
data.Remove(obj);
所以也许我也需要从localyKeyWords中删除obj?
我得到的错误解释是,在它从LocalyKeyWords中删除项目后,点击继续这一行:
foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
我收到错误:
集合已修改;枚举操作可能无法执行
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Collection was modified; enumeration operation may not execute.
Source=mscorlib
StackTrace:
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
at GatherLinks.Form1.listBox1_KeyDown(Object sender, KeyEventArgs e) in d:'C-Sharp'GatherLinks'GatherLinks'GatherLinks'Form1.cs:line 959
at System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at GatherLinks.Program.Main() in d:'C-Sharp'GatherLinks'GatherLinks'GatherLinks'Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
您正在从LocalyKeyWords
中删除项,同时正在对其进行迭代;正如异常消息所说,这是不允许的。
我不确定这里的大局是什么,但本地化的解决方案是制作LocalyKeyWords
的临时副本并对其进行迭代。然后,您可以修改"源"集合,而不会出现任何问题。
示例:
foreach (var kvp in LocalyKeyWords.ToList()) // .ToList() makes a temp copy
您不能修改foreach
内部的IEnumerable集合。
您可以创建字典的副本,并在其中循环修改原始字典。