7

I get this error when I try to open a form in my c# .NET Framework 4.7.2 class library. Auto-generate binding redirects is checked

The call stack is

at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit)
at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectGetAttributes(Type type)
at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectedTypeData.GetAttributes()
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetAttributes()
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetAttributes()
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetAttributes()
at System.ComponentModel.TypeDescriptor.GetAttributes(Type componentType)
at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectedTypeData.GetConverter(Object instance)
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetConverter()
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetConverter()
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetConverter()
at System.ComponentModel.TypeDescriptor.GetConverter(Type type)
at System.ComponentModel.PropertyDescriptor.get_Converter()
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetCollectionOfBoundDataGridViewColumns()
at System.Windows.Forms.DataGridView.RefreshColumns()
at System.Windows.Forms.DataGridView.RefreshColumnsAndRows()
at System.Windows.Forms.DataGridView.OnBindingContextChanged(EventArgs e)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.Form.ControlCollection.Add(Control value)
at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c) 

I looked at this question and tried adding to following to app.config of the .exe but it did not help

  <dependentAssembly>
    <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="4.2.0.0" newVersion="4.7.0.0" />
  </dependentAssembly>

The actual form is in a dll so I also tried adding it to the app.config of the dll

In my project file I have

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>  
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

I see that 4.2.0 is not even available on Nuget.

enter image description here

I wonder if I can turn off the attempt to call

DataGridView.DataGridViewDataConnection.GetCollectionOfBoundDataGridViewColumns()

at design time

It does seem safe to just click Ignore And Continue.

Team Explorer Changes indicates that this does not change the code.

[Update]

If I try to copy and paste the DataGridView control to another form I get An error occured while processing this command. Could not load file or assembly an error occurred

I was able to add a new DataGridView control from the toolbox however when I tried to attach a binding source to that I got

Error setting value MyBindingSource to property DataSource

Error setting value

[Update]

I discovered the project data source for the binding source had been deleted. That is when I clicked the down arrow on the binding source's datasource property, the datasource was not in the list to pick from.

It turned out that I had forgotten to include the project properties DataSources in the source control.

However when I add a new DataGridView and try to set choose the binding source I still get the error could not load file or assembly

The binding source is based on an object. The error happens for a particular object and not another. Perhaps it is a property of the object that is causing an issue.

[Update]

The issue occurs when the bound object contains a property that is also an object

public class BOMObj
    {
       // public FramePart FramePart { get; set; } // error occurs if I uncomment this
        public string PartTypeName => $"{FramePart?.ComponentType}"; 
        public string thing3 => "thing 3";
        public string thing { get; set; }
        public  string thing2 { get; }

I tried instantiating FramePart in the constructor but it did not help

   public BOMObj() {
        FramePart = new FramePart();
    }

I suppose it makes sense that this code cannot run at design time.. but why the missing file message ?

[Update] Because of this question I feel pretty confident that my redirections are ok. Note the issue is happening only at design time.

[Update]

Looking in Process Monitor for System.ComponentModel.Annotations

ProcMon capture

But could not see any Failure messages.

I also tried installing version 4.1.0 which led to warning

NU1605  Detected package downgrade: System.ComponentModel.Annotations from 4.7.0 to 4.1.0. Reference the package directly from the project to select a different version. 
Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/design-time-errors-in-the-windows-forms-designer – Kirsten May 27 '20 at 19:47
  • Thanks, but it does not help – Kirsten May 30 '20 at 22:36
  • Fusion Log Viewer (fuslogvw.exe) output may help to figure out additional details. – Oleg May 31 '20 at 09:52
  • Thanks Oleg, I updated the end of the question. The issue happens at design time only. – Kirsten Jun 01 '20 at 04:29
  • Try to reinstall NuGet packages. See: [How to reinstall and update packages](https://learn.microsoft.com/en-us/nuget/consume-packages/reinstalling-and-updating-packages) This should help you to remove bad entries. – Maciej Los Jun 02 '20 at 10:23
  • I tried it, but it did not help – Kirsten Jun 02 '20 at 18:04
  • What are the dependencies for System.ComponentModel.Annotations dll. It's quiet possible that the transitive dependencies are missin.g – Soundararajan Jun 05 '20 at 16:59
  • Does Visual Studio show a warning message with the "MSB3277" code indicating an assembly version conflict? If so, try adding the NuGet package of the name of the assembly in conflict to the Project specified in the warning's Project column. – egnomerator Jun 05 '20 at 20:25
  • Is your particular object that doesn't work in a .NET Standard library? I have found when BindingSources are used with an object within a .NET Standard library, we get that design-time error and the app.config redirects don't seem to get honoured. But if the object is in a .NET Framework library, it is OK. – mhapps Apr 27 '21 at 09:11

1 Answers1

1

Try doing a repair of the .Net Framework, start > run > appwiz.cpl and select the option in .Net to Repair.

Next the first error message is "Could not load file or assembly". Where is it looking for this DLL? Run Process Monitor and check the path Visual Studio is looking for it. Either put the DLL/Assembly where its expected to be found or Re-Reference it again. Try rolling back to 4.1 and using that NuGet version or try a later package like 4.4.1.

Install-Package System.ComponentModel.Annotations -Version 4.1.0

All these people commenting get different results depending on the version: https://stackoverflow.com/a/44079161/495455

The second error is the same root cause as the first error, so first diagnose and solve the root cause and avoid troubleshooting symptoms of the problem.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Thanks, I updated the question. I can't see any failures in ProcMon to work out where it is looking – Kirsten Jun 06 '20 at 00:14