无法加载 System.Resources.Extensions - 在 .Net Framework 中

Dav*_*len 7 exception winforms typeinitializationexception

我的代码中突然出现错误,该代码是为 .Net Framework 4.6.1 编译的 Forms 应用程序。当我去创建一个继承自 TreeView 对象的对象时,就会发生这种情况。

它在 TreeView 之上做的事情很少。

例外的是:

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>
Run Code Online (Sandbox Code Playgroud)

它有这样的代码:

protected override void OnHandleCreated(EventArgs e)
    {
    SendMessage(this.Handle, TVM_SETEXTENDEDSTYLE, (IntPtr)TVS_EX_DOUBLEBUFFER, (IntPtr)TVS_EX_DOUBLEBUFFER);
    base.OnHandleCreated(e);
    }
Run Code Online (Sandbox Code Playgroud)

并在设置中执行以下操作:

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);
}
Run Code Online (Sandbox Code Playgroud)

它有成员变量,但它们都是标准的 Forms 类:

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;
Run Code Online (Sandbox Code Playgroud)

知道出了什么问题吗?

小智 10

这是由于 .net 4.8 及之前版本中存在的错误所致,请参阅https://github.com/dotnet/runtime/issues/39078

我在引用为 .net core 3.1 和 .net Framework 4.8 构建的库时得到了它。

请注意,在针对框架和 .net core 时,您需要引用 System.Resources.Extensions nuget。添加了一个新的项目属性<GenerateResourceUsePreserializedResources>True</GenerateResourceUsePreserializedResources>,因此资源捆绑适用于两者。

解决方法(来自错误评论并为我工作)是向 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>
Run Code Online (Sandbox Code Playgroud)