简体   繁体   English

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

[英]Can't load System.Resources.Extensions - in .Net Framework

I am suddenly getting an error in my code which is a Forms app compiled for .Net Framework 4.6.1.我的代码突然出现错误,这是为 .Net Framework 4.6.1 编译的 Forms 应用程序。 It occurs when I go to create an object that inherits from the TreeView object.当我去创建一个继承自 TreeView 对象的对象时会发生这种情况。

It does very little on top of TreeView.它在 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:它有成员变量,但它们都是标准的 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;

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这是由于 .net 4.8 和之前存在的错误,请参阅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.我在引用为 .net core 3.1 和 .net framework 4.8 构建的库时得到了它。

Note that you need to reference the System.Resources.Extensions nuget when targeting both framework and .net core.请注意,当同时针对框架和 .net 核心时,您需要引用 System.Resources.Extensions nuget。 There is a new project property <GenerateResourceUsePreserializedResources>True</GenerateResourceUsePreserializedResources> that is added so resources bundling works for both.添加了一个新的项目属性<GenerateResourceUsePreserializedResources>True</GenerateResourceUsePreserializedResources> ,因此资源捆绑适用于两者。

Workaround (from the bug comments and worked for me) is to add a bindingredirect to app.config解决方法(来自错误评论并为我工作)是向 app.config 添加 bindingredirect

  <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM