我有两个ASP。NET Web项目(ProjectA和projecb)。当ProjectA中的类正在实例化ProjectB中的一个类时,它使用了一个资源文件Blah。resx,我得到这个错误:

一个System.Resources类型的异常。mscorlib.dll中出现了MissingManifestResourceException',但在用户代码中没有处理。 无法找到适用于指定区域性或中性区域性的任何资源。确保“资源。等等。”资源“正确嵌入或链接到程序集”App_GlobalResources。Sn_flri6”,或者所有所需的附属程序集都是可加载的并具有全符号。

是什么导致的?

在微软的网站上有一篇关于http://support.microsoft.com/kb/318603的文章,它建议:

要解决此问题,请移动所有其他类定义,使它们出现在表单的类定义之后。

这是一个Windows窗体项目的解决方案,我不确定这是否也适用于Web项目。


当前回答

一种方法是将共享的类/资源放在单独的类库项目中,并在两个网站中引用它们。

其他回答

就这种情况而言,检查包含资源的程序集是否将默认名称空间设置为相同的文本(Project->Properties->默认名称空间;在VS) 检查resx文件是否有BuildAction属性设置为“嵌入式资源” 享受……;)

谢谢@CFinck ! 只是给其他人添加一个提示:我用这个改变了ResourceManager行:

New Global.System.Resources.ResourceManager(Reflection.Assembly.GetCallingAssembly.GetName.Name & ".CommonNameOf.Resources", Reflection.Assembly.GetCallingAssembly())

我在vb.net,但我认为在c#中唯一的区别是+而不是&连接字符串。

这样,我就可以在两个共享资源的类似项目中使用相同的链接程序集文件。

在我的例子中,这个问题是由错误的类定义顺序引起的。例如,我在Form类之前添加了另一个类定义:

namespace MyBuggyWorld
{
    public class BackendObject //This hack broke the VS 2017 winform designer and resources linker!
    {
        public TcpClient ActiveClient { get; set; }
        public BackgroundWorker ActiveWorker { get; set; }
    }

    public partial class FormMain : Form
    {
    }
}

在移动BackendObject到文件的末尾(更好的是将其移动到一个单独的文件),做项目清洁+重建解决了这个问题。

我发现删除designer.cs文件,从项目中排除resx文件,然后重新包含它通常会修复这类问题,遵循命名空间重构(根据CFinck的回答)

This can be caused by mismatched namespaces. The second from top answer (Sibi Elango's) says to right-click the resx file, and change Build option to EmbeddedResource, but I had already done that and still had the error. The top answer (CFinck's) notes a way of fixing this via manually editing files, however, I had this problem in MonoDevelop, and had to set the default namespace to the same as the cs file which was calling for the resource (the file which contained code such as the code below)...

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

通过GUI设置默认名称空间后,上面的代码行不再引起异常。