Resource files (resx) does not respect Custom Tool Namespace(资源文件(Resx)不符合自定义工具命名空间)
问题描述
在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(typeof(CommentResources))
并尝试访问键时,它抛出MissingManifestResourceException,,例如:
var manager = new ResourceManager(typeof(CommentResources));
var resource = manager.GetString("myKey");
如何才能真正更改命名空间?
编辑:
看看我下面的解决方案。每当我在Enviroment
文件夹中创建resx文件时,它都会创建一个不需要的命名空间。这就是我想要避免的
推荐答案
我最近遇到了同样的问题。
Visual Studio2017似乎生成了从RootNamespace.SubFolder.ResourcesFileName而不是CustomToolNamespace.ResourcesFileName.创建ResourceManager的代码由于代码是从名为ResXFileCodeGenerator(用于内部类)或PublicResXFileCodeGenerator(用于公共类)的Visual Studio的Single-File Generator工具创建的,该工具内部使用StronglyTypedResourceBuilder类,因此除了将您自己的Single-File Generator实现为Visual Studio扩展并将其用作EmbeddedResource的生成器外,无法自定义其行为。
幸运的是,有一个更简单的解决方法。在.csproj文件中的EmbeddedResource标记下,使用文本值RootNamespace.SubFolder.ResourcesFileName.resources.指定LogicalName标记在您的特定案例中,它将如下所示:
<LogicalName>DefaultNamespace.CommentResources.resources</LogicalName>
这篇关于资源文件(Resx)不符合自定义工具命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:资源文件(Resx)不符合自定义工具命名空间
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01