[英]C# How can I used a string to reference a pre-existing variable by the same name?
I have a textfile and on each line is a single word followed by specific values 我有一个文本文件,每一行上都是一个单词,后跟特定的值
For example: 例如:
texture_menu_label_1 = 0 0 512 512
What I want to do is read that text in and basically convert it to the following commmand: 我想做的是读入该文本并将其基本上转换为以下命令:
texture_menu_label_1 = new int[]{0, 0, 512, 512};
Parsing the line and extracting the integer values for the constructor is trivial, but im wondering if there is anyway to use the "texture_menu_label_1" String from the file to reference a pre-existing variable by the same name... 解析行并为构造函数提取整数值是微不足道的,但是我想知道是否仍然可以使用文件中的“ texture_menu_label_1”字符串来引用同名的预先存在的变量...
Is there anyway to do this without manually constructing a lookup table? 无论如何,无需手动构造查找表就能做到这一点?
You really don't want to do this. 您真的不想这样做。 I know you think you do, I remember when I was learning how to program and I thought the same thing, but really, you don't.
我知道您认为您确实如此,我记得我在学习编程的时候也曾想过同样的事情,但实际上,您没有。
There are better ways to store a collection values, in your case, this would be a multi-dimensional array (or a List<List<int>>
). 有更好的方法来存储集合值,在您的情况下,这将是一个多维数组(或
List<List<int>>
)。 If not that, then perhaps a hash table ( Dictionary<string,int[]>
). 如果不是那样,则可能是一个哈希表(
Dictionary<string,int[]>
)。
Better yet, if this data is 'regular' and logically connected, create your own custom type and maintain a collection of those. 更好的是,如果此数据是“常规”的并逻辑连接的,则创建您自己的自定义类型并维护这些类型的集合。 You really don't want to go down the road of tying your logic to the names of your variables... very messy.
您真的不想走将逻辑与变量名联系起来的道路……非常混乱。
That data looks like a rectangle. 该数据看起来像一个矩形。 Why not just maintain a
Dictionary<string,Rectangle>
? 为什么不只维护
Dictionary<string,Rectangle>
呢?
var dict = new Dictionary<string, Rectangle>();
dict.Add("some_name", new Rectangle(0, 0, 512, 512));
// ... later
var rect = dict["some_name"]; // get the rectangle that maps to "some_name"
Before you try to implement an answer please consider why you are doing this, and whether there may be a better solution. 在尝试实施答案之前,请考虑为什么要这样做,以及是否有更好的解决方案。
I recommend using a Dictionary to store the data by name as strings. 我建议使用字典按名称将数据存储为字符串。
dataDictionary["texture_menu_label_1"] = new int[] { ... };
Another approach is to use a separate class with fields, since fields can be accessed by name. 另一种方法是对字段使用单独的类,因为可以通过名称访问字段。 You may experience performance issues though, and it's definitely not an optimal solution.
您可能会遇到性能问题,但这绝对不是最佳解决方案。
class Data
{
public int[] texture_menu_label_1;
...
}
You can use reflection to set the field value. 您可以使用反射来设置字段值。 Something like this:
像这样:
typeof(Data).GetField("texture_menu_label_1").SetValue(data, new int [] { ... });
Use a HashTable (Dictionary for generics) or similar. 使用HashTable(通用字典)。 The key would be the string (texture_menu_label_1) and the value would be the array.
密钥将是字符串(texture_menu_label_1),而值将是数组。
What if you wrap it up in a struct? 如果将其包装在结构中怎么办?
struct TextureMenu
{
string MenuString;
int[] Values;
}
Then, instead of dealing directly with either type, you just deal with the struct. 然后,您不必处理任何一种类型,而只需处理该结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.