.net ResourceManager 试图加载 .resources 而不是 .resx 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1460981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:21:47  来源:igfitidea点击:

ResourceManager trying to load .resources not .resx file

.netresourcemanager

提问by Adam Fox

I am trying to load a resx file in my .net website using:

我正在尝试使用以下方法在我的 .net 网站中加载 resx 文件:

ResourceManager rm = new ResourceManager( "Project.Resource", Assembly.GetExecutingAssembly() );

The Resource.resx file is in the folder App_LocalResources and is set to be embedded in assembly on build.

Resource.resx 文件位于文件夹 App_LocalResources 中,并设置为在构建时嵌入到程序集中。

When I try to access the resx file using:

当我尝试使用以下方法访问 resx 文件时:

rm.GetString( "key" );or rm.GetString( "key", culture );

rm.GetString( "key" );或者 rm.GetString( "key", culture );

I get an error message:

我收到一条错误消息:

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Project.Resource.resources" was correctly embedded or linked into assembly "Project" at compile time, or that all the satellite assemblies required are loadable and fully signed.

找不到适合指定文化或中性文化的任何资源。确保在编译时将“Project.Resource.resources”正确嵌入或链接到程序集“Project”中,或者确保所有所需的附属程序集都可加载并已完全签名。

Notice the .resources ... any ideas on what is going on here?

请注意 .resources ... 关于这里发生的事情的任何想法?

采纳答案by Ganesh R.

I think the way you are using ResourceManager is wrong. See this post.

我认为您使用 ResourceManager 的方式是错误的。看到这个帖子

Also note, when you open Visual Studio command prompt, & run resgen.exe, it says its used to convert resource files from one format to another (i.e. resx to resources). I think, you will need to convert your file to resources from resx & then load it using resourceManager.

另请注意,当您打开 Visual Studio 命令提示符并运行 resgen.exe 时,它​​说它用于将资源文件从一种格式转换为另一种格式(即 resx 到资源)。我认为,您需要将文件从 resx 转换为资源,然后使用 resourceManager 加载它。

回答by diimdeep

To load .resx into ResourceManager you need specify namespace

要将 .resx 加载到 ResourceManager 中,您需要指定命名空间

var rm = new ResourceManager("Namespace.ResxName", Assembly.GetAssembly());

or you can get ResourceManager for free if set Access Modifier inside Managed Resource Editor to Internal or Public, after that VS will generate ResxName.Designer.cs

或者你可以免费获得ResourceManager,如果将Managed Resource Editor 中的Access Modifier 设置为Internal 或Public,之后VS 将生成ResxName.Designer.cs

var rm = ResxName.ResourceManager;

回答by Landeeyo

There's surprisingly simple way of reading resource by string:

按字符串读取资源的方法非常简单:

ResourceNamespace.ResxFileName.ResourceManager.GetString("ResourceKey")

It's clean and elegant solution for reading resources by keys where "dot notation" cannot be used (for instance when resource key is persisted in the database).

对于无法使用“点符号”的键读取资源(例如,当资源键保留在数据库中时),这是一种干净而优雅的解决方案。

回答by Ganesh R.

I'm not sure which version of .NET Framework are you using.

我不确定您使用的是哪个版本的 .NET Framework。

Try channging the way how you bring the ResourceManager to life.

尝试改变让 ResourceManager 栩栩如生的方式。

ResourceManager rm = 
     new ResourceManager("Project.Resource", 
                         System.Reflection.Assembly.Load("App_LocalResources"));

It should work.

它应该工作。

This is also exposed as a static property of the automatically generated .designer.cs class of the concrete resorce manager.

这也作为具体资源管理器自动生成的 .designer.cs 类的静态属性公开。

回答by Jon Seigel

Add the .resx extension explicitly.

显式添加 .resx 扩展名。

You can also use the auto-generated class and use its properties if that is suitable for your project.

如果适合您的项目,您还可以使用自动生成的类并使用其属性。