Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly System.Componentmodel.Annotations 4.2.0.0 in designer

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. 
like image 611
Kirsten Avatar asked May 27 '20 17:05

Kirsten


1 Answers

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.

like image 183
Jeremy Thompson Avatar answered Sep 18 '22 15:09

Jeremy Thompson