I am suddenly getting an error in my code which is a Forms app compiled for .Net Framework 4.6.1. It occurs when I go to create an object that inherits from the TreeView object.
It does very little on top of TreeView.
The exception is:
System.IO.FileNotFoundException
HResult=0x80070002
Message=Could not load file or assembly 'System.Resources.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
It has this code:
protected override void OnHandleCreated(EventArgs e)
{
SendMessage(this.Handle, TVM_SETEXTENDEDSTYLE, (IntPtr)TVS_EX_DOUBLEBUFFER, (IntPtr)TVS_EX_DOUBLEBUFFER);
base.OnHandleCreated(e);
}
And does the following in setup:
private void InitializeComponent()
{
this.SuspendLayout();
//
// LinkTreeView
//
this.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawText;
this.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.LinkTreeView_DrawNode);
this.MouseEnter += new System.EventHandler(this.LinkTreeView_MouseEnter);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.LinkTreeView_MouseMove);
this.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.LinkTreeView_NodeMouseClick);
this.MouseLeave += new System.EventHandler(this.LinkTreeView_MouseLeave);
this.ResumeLayout(false);
}
It has member variables, but they're all standard Forms classes:
private Brush disabledBrush;
private Brush foregroundBrush;
private Brush linkBrush;
private Brush visitedBrush;
private Brush backGroundBrush;
private Pen activeLinkPen;
private Pen linkPen;
private Pen visitedLinkPen;
private StringFormat format;
// the delete bitmap out at the end.
private static readonly Bitmap deleteActive;
private static readonly Bitmap deleteDisabled;
private static readonly Rectangle rectDeleteBitmap;
Any idea what is going wrong?
This is due to a bug that exists in .net 4.8 and before see https://github.com/dotnet/runtime/issues/39078
I got it when referencing a library built for .net core 3.1 and .net framework 4.8.
Note that you need to reference the System.Resources.Extensions nuget when targeting both framework and .net core. There is a new project property <GenerateResourceUsePreserializedResources>True</GenerateResourceUsePreserializedResources>
that is added so resources bundling works for both.
Workaround (from the bug comments and worked for me) is to add a bindingredirect to app.config
<dependentAssembly>
<assemblyIdentity name="System.Resources.Extensions" culture="neutral" publicKeyToken="cc7b13ffcd2ddd51" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>