动态参考资源文件在C# [英] Dynamic reference to resource files in C#
问题描述
我有哪些我采取本地化的应用程序。
I have an application on which I am implementing localization.
我现在需要在种源文件动态引用一个名称。
I now need to dynamically reference a name in the resouce file.
假设我有一个名为Login.resx资源文件,一个一个数字的字符串:富=你好,酒吧=残酷和巴兹=世界
assume I have a resource file called Login.resx, an a number of strings: foo="hello", bar="cruel" and baz="world"
通常,我会参考:
字符串结果= Login.foo;
和结果==你好;
normally, I will refer as: String result =Login.foo; and result=="hello";
我的问题是,在那个时候的代码,我不知道如果我想引用富,酒吧或巴兹 - 我有一个包含任何富,酒吧或巴兹的字符串。
my problem is, that at code time, I do not know if I want to refer to foo, bar or baz - I have a string that contains either "foo", "bar" or "baz".
我需要的是这样的:
登录[富];
有谁知道是否有什么办法可以在资源文件动态引用字符串?
Does anyone know if there is any way to dynamically reference a string in a resource file?
推荐答案
你需要实例 的ResourceManager
为 Login.resx
:
var resman = new System.Resources.ResourceManager(
"RootNamespace.Login",
System.Reflection.Assembly.GetExecutingAssembly()
)
var text = resman.GetString("resname");
这可能有助于看看那些资源文件的代码隐藏文件生成的代码由IDE创建的。这些文件主要包含,使得查询到内部资源管理器中每个资源的只读属性。
It might help to look at the generated code in the code-behind files of the resource files that are created by the IDE. These files basically contain readonly properties for each resource that makes a query to an internal resource manager.
这篇关于动态参考资源文件在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!