C# 用字符串获取资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17828774/
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-08-10 10:35:55  来源:igfitidea点击:

Get Resources with string

c#

提问by 1teamsah

I have a lot of txt files in Resources folder. One of them is corner.txt. I can access this file via this code snippet:

我在 Resources 文件夹中有很多 txt 文件。其中之一是corner.txt。我可以通过这个代码片段访问这个文件:

Properties.Resources.corner

I keep file names in string variables. For example:

我将文件名保存在字符串变量中。例如:

string fileName = "corner.txt";

I want to access this file via:

我想通过以下方式访问此文件:

Properties.Resources.fileName 

Is this possible? How can I access?

这可能吗?如何访问?

采纳答案by 1teamsah

I solved the problem this code snippet:

我解决了这个代码片段的问题:

string st = Properties.Resources.ResourceManager.GetString(tableName);

So, I don't use the filename, I use the txt file's string. This is useful for me.

所以,我不使用文件名,我使用 txt 文件的字符串。这对我很有用。

Thanks a lot.

非常感谢。

回答by Swift

You can use Reflectionlike that:

您可以像这样使用反射

var type = typeof(Properties.Resources);
var property = type.GetProperty(fileName, BindingFlags.Static| BindingFlags.NonPublic|BindingFlags.Public);
var value = property.GetValue(null, null);

or use the ResourceManagerlike that:

或使用ResourceManager类似的:

value = Properties.Resources.ResourceManager.GetObject(fileName, Properties.Resources.Culture);