根据key返回资源
根据我的上一个问题,嵌入资源似乎无法在我的项目。因此计划#2 是通过 resx 文件使用常规资源。
我的问题的核心是:是否可以编写一个接受字符串键并返回该资源的函数?我用反射尝试了这一点,但无法让它工作。
这是我希望它如何工作的示例。假设我有一个 Resources.resx
文件,其中包含两个文件资源:MainMap
和 OverWorld
。我想编写这样的函数:
string mainMapContent = getFromResources("MainMap"); // => returns Resources.MainMap
string overWorldCOntent = getFromResoures("OverWOrld"); // => returns Resources.OverWorld
我尝试使用反射来创建 Resources
类的实例,但当我意识到构造函数是内部的并且没有可以使用的空构造函数时,我就放弃了。
有没有办法编写这个 getFromResources 函数?我想不通。
注意:如果可以的话,我可能会将其放入库中;它也需要与 Silverlight 运行时一起工作。
Based on my previous question, it looks like emedded resources are not going to work in my project. So plan #2 is to use regular resources via a resx file.
The core of my question is: is it possible to write a function that will take a string key, and return that resource? I tried this with reflection, but I couldn't get it to work.
Here's a sample of how I would like it to work. Let's say I have a Resources.resx
file, which has two file resources: MainMap
and OverWorld
. I would like to write the function that works like:
string mainMapContent = getFromResources("MainMap"); // => returns Resources.MainMap
string overWorldCOntent = getFromResoures("OverWOrld"); // => returns Resources.OverWorld
I tried using reflection to create an instance of the Resources
class, but bailed out when I realized the constructor is internal and there's no empty constructor I can use.
Is there a way to write this getFromResources
function? I can't figure it out.
Note: I will probably put this into a library if I can do it; it needs to work with the Silverlight runtime, too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过代码生成生成的强类型资源类基于非类型化 ResourceManager 类。您应该能够使用 ResourceManager.GetObject
The strongly typed resource class that is generated through code-generation is based on the untyped ResourceManager class. You should be able to use ResourceManager.GetObject