简体   繁体   English

C#如何使用字符串引用具有相同名称的预先存在的变量?

[英]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.

相关问题 如何从字符串中拉出变量我需要与另一个预先存在的字符串C#进行比较 - How to pull a variable out of a string I need to compare to another, pre-existing string c# 我可以在C#中使用的快速预先存在的可嵌入脚本语言 - Fast Pre-existing embeddable scripting language I can use in C# 我可以将预先存在的 Javascript 代码添加到 ASP.NET MVC C# 应用程序吗 - Can I add pre-existing Javascript code to an ASP.NET MVC C# application 如何在功能区(C#)上的预先存在的选项卡中添加按钮? - How to add a button to a pre-existing tab on ribbon (C#)? 如何在 C# 中为预先存在的时间添加额外的小时数? - How to add additional hours to pre-existing time in C#? 如何使用C#中预先存在的Windows窗体在选项卡控件中动态创建选项卡页 - How to dynamically create Tab Pages in a Tab Control with a pre-existing Windows Form in C# C#:如何使用字符串变量的内容调用同名方法? - C#: How can I use the content of a string variable to call a method with the same name? 如何将通用列表设置为相同类型的现有列表? - how to set generic list to a pre-existing list of the same type? 我可以将现有的iTextSharp文档转换为字节数组吗 - Can I convert a pre-existing iTextSharp Document to a byte array 使用预先存在的私钥对文件签名以创建证书(C#) - Using a pre-existing private key to sign file to create a certificate (C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM