The collection has been modified; enumeration operation may not be performed in VS WinForms Designer

Since converting our inhouse winforms application from the VS2008 project to VS2012, I have been having problems using the winforms constructor.

Sometimes a designer gets into an error state, causing the following error message:

"The collection has been modified; an enumeration operation cannot be performed." with a call stack saying:

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) 

It seems that the designer is trying to change the list of reference assemblers, possibly due to some invalid builds. When I tried to solve the problem, I realized that changing the "Enable ClickOnce Security function" option in Project-> Properties-> Security can cause the designer to return to work. BUT, if the option is enabled, and I get a constructor error, turning it off and restoring everything, from time to time VICE VERSA can solve the problem !!!! ... That's why I got a little lost right now.

+7
source share
2 answers

I had the same problem that drove me crazy all day. The reason (of course, due to a Visual Studio error) was that there was no assembly reference in my project. To be precise, he referenced two assemblies A and B, where A himself also referenced B, but in a different version from the one I used. I should have referenced the same version of B referenced by A.

What it boils down to: My assembly references were not quite correct. They were good enough for the code to compile and execute just fine, but wrongly enough for the WinForms constructor to crash.

To find the problem, I launched the second instance of VS and bound its debugger to the first one, from which my solution was downloaded (Debug | Attach to Process). Then in Debug | Exceptions, I have activated Common Language Runtime Exceptions. Now, in my first instance of VS, I double-clicked the form to open it.

I got a few exceptions per line, most of which have nothing to do with my code. But one of them was the FileLoadException exception: "Could not load the file or assembly" XXX, Version = 3.7.0.25089, ... "Therefore, I added a link to the correct version of the assembly. (And, rather, I mean to the last digit - WinForms designer is awfully pedantic when it comes to build versions.) And, what else do I know, the form opens like a charm!

+4
source

This seems to be a bug in Visual Studio 2012.

Here is the official mail I received from Microsoft

Hello from Microsoft Connect!

This notification was created for a feedback element: VS2012 WinForms Designer no longer works -Error: collection was changed; The transfer operation could not be performed. that you submitted to the Microsoft Connect website.

Thank you for your feedback. This error is the duplicate listed here: http://connect.microsoft.com/VisualStudio/feedback/details/781193/form-designer-error-collection-was-modified-enumeration-operation-may-not-executethere Please track this issue (781193) for further updates. There are no workarounds, but we are fixing this problem in the next version of Visual Studio.

+1
source

All Articles