【问题标题】:Resource files (resx) does not respect Custom Tool Namespace资源文件 (resx) 不尊重自定义工具命名空间
【发布时间】:2017-04-23 17:03:10
【问题描述】:

在 resx 属性中,我将自定义工具命名空间从 DefaultNamespace 更改为 MyNamespace.Language 并生成以下代码:

namespace MyNamespace.Language
{
  public class CommentResources
  {
    public static global::System.Resources.ResourceManager ResourceManager {
      get {
           //removed code...
           global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DefaultNamespace.CommentResources", typeof(CommentResources).Assembly);
  }
}

如您所见,仅更改了类命名空间,但没有更改 ResourceManager 构造函数中传递的命名空间,因此,当我实例化 ResourceManager(typeof(CommentResources)) 并尝试访问密钥时,它会抛出 MissingManifestResourceException强>,例如:

var manager = new ResourceManager(typeof(CommentResources));
var resource = manager.GetString("myKey");

我怎样才能真正改变命名空间?

编辑

看看我下面的解决方案。每当我在 Enviroment 文件夹中创建 resx 文件时,它都会创建一个不需要的命名空间。这就是我要避免的事情

【问题讨论】:

  • 这个完全符合医生通常的建议:“如果疼就不要这样做”。您必须解释为什么更改命名空间对您很重要,以及如果您想要替代方法,为什么更改项目的默认命名空间设置还不够好。
  • 这还不够好。在我的一个项目(csproj)中,我必须为每个类创建一个 resx,并且文件夹中有几个类,但是这些类没有“文件夹”命名空间,resx 也不应该,所以这就是原因。例如,有一个名为 Enviroment 的文件夹,我不能将它作为命名空间的一部分,因为你知道,Enviroment 已经是一个类。这只是一个例子,还有其他几个类似的文件夹。
  • 我们有很好的退款保证。请不要告诉我,将其添加到您的问题中。详细说明,尤其是“文件夹”细节。

标签: c# .net visual-studio namespaces manifest


【解决方案1】:

我最近偶然发现了同样的问题。

似乎 Visual Studio 2017 生成的代码从 RootNamespace.SubFolder.ResourcesFileName 而不是 CustomToolNamespace.ResourcesFileName 创建 ResourceManager

由于代码是从 Visual Studio 的 Single-File Generator 工具创建的,该工具称为 ResXFileCodeGenerator(用于内部类)或 PublicResXFileCodeGenerator(用于公共类),在内部使用 StronglyTypedResourceBuilder 类,因此无法自定义其行为,除了将您自己的 Single-File Generator 实现为 Visual Studio 扩展并将其用作 EmbeddedResource 的生成器。

幸运的是,有一个更简单的解决方法。在 .csproj 文件中的 EmbeddedResource 标签下,使用 RootNamespace.SubFolder.ResourcesFileName.resources 的文本值指定 LogicalName 标签。

在您的具体情况下,它看起来像这样:

<LogicalName>DefaultNamespace.CommentResources.resources</LogicalName>

【讨论】:

  • 原来我错了...ResourceManager("DefaultNamespace.CommentResources", typeof(CommentResources).Assembly) 是对的,这不是错误。第一个参数 (DefaultNamespace.CommentResources) 是程序集中嵌入 resx 文件的路径
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-20
  • 2010-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多