c# - 集合被修改;枚举操作可能无法在 VS WinForms Designer 中执行

标签 c# winforms designer

自从将我们公司内部的 winforms 应用程序从 VS2008 项目转换为 VS2012 项目后,我在使用 winforms 设计器时遇到了问题。

有时设计者会陷入错误状态并给出以下错误信息:

“集合已修改;枚举操作可能无法执行。”调用堆栈说:

Instances of this error (1)  

1.   Hide Call Stack 

at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblySpecFound(List`1 assemblies, String assemblyFullName)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.AddDependencies(Assembly a, String fileName)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblyEntry.get_Assembly()
at Microsoft.VisualStudio.Design.VSTypeResolutionService.SearchByShortName(String partialName, String fullName, AssemblyEntry[] entries, Assembly& assembly)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.SearchNormalEntries(AssemblyName assemblyName, String typeName, Boolean ignoreTypeCase, Assembly& assembly, Boolean fastSearch)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.SearchEntries(AssemblyName assemblyName, String typeName, Boolean ignoreCase, Assembly& assembly, ReferenceType refType)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.SearchEntries(AssemblyName assemblyName, String typeName, Boolean ignoreTypeCase, Assembly& assembly)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.System.ComponentModel.Design.ITypeResolutionService.GetAssembly(AssemblyName name, Boolean throwOnError)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.System.ComponentModel.Design.ITypeResolutionService.GetAssembly(AssemblyName name)
at Microsoft.VisualStudio.Design.VSDynamicTypeService.OnAssemblyResolve(Object sender, ResolveEventArgs e)
at System.AppDomain.OnAssemblyResolveEvent(RuntimeAssembly assembly, String assemblyFullName)  

似乎设计者试图更改引用程序集的列表可能是因为某些无效程序集。 在尝试解决该问题时,我发现更改“项目”->“属性”->“安全”下的“启用 ClickOnce 安全”选项可以使设计人员恢复工作。但是,如果该选项已打开并且我收到设计器错误,请将其关闭并重建所有这些都可以不时解决问题,反之亦然!!!! ... 这就是为什么我现在有点迷路。

最佳答案

我遇到了同样的问题,这让我整天发疯。原因(当然除了 Visual Studio 错误本身)是我的项目缺少程序集引用。准确地说,它引用了两个程序集 A 和 B,其中 A 本身也引用了 B - 但与我使用的版本不同。我应该引用 A 引用的相同版本的 B。

归根结底是:我的程序集引用不太正确。它们正确到足以让代码编译和运行得很好,但错误到足以让 WinForms 设计器崩溃。

为了找到问题所在,我启动了第二个 VS 实例并将其调试器附加到第一个加载了我的解决方案的实例(调试 | 附加到进程)。然后在 Debug |异常,我激活了中断通用语言运行时异常。现在,在我的第一个 VS 实例中,我双击表单将其打开。

我连续收到多个异常,其中大部分与我的代码无关。但其中之一是 FileLoadException 声明:“无法加载文件或程序集 'XXX,版本 = 3.7.0.25089,...”因此我添加了对程序集正确版本的引用。 (正确的是,我的意思是直到最后一位数字 - WinForms 设计器在汇编版本方面非常迂腐。)接下来我知道,表单打开时就像一个魅力!

关于c# - 集合被修改;枚举操作可能无法在 VS WinForms Designer 中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16477960/

相关文章:

c# - 如何使用 OneDrive SDK 按文件哈希搜索

c# - VS .NET 自定义组件中的属性编辑样式为 DropDown

c# - 从 Main() 返回字符串变量

c# - 将我的数据库绑定(bind)到表单控件时出现 SQL Server 错误

c# - 在 C# Winforms 应用程序中从命令提示符收集数据的好方法是什么?

c# - 迭代循环并在两个表达式之间切换,唯一的区别是符号

WPF设计器异常; "An item with the same key had already been added"

wpf - WPF 设计器中的字段选择 - 没有下拉菜单?

c# - 在单元测试中使用动态

c# - UserNamePasswordValidator 可以抛出 MessageSecurityException 以外的任何东西吗?