在 C# 中使用字符串作为 class 的属性名称

Using a string as a name of an attribute of a class in C#

问题可能已经得到解答,但老实说我不知道​​要搜索什么,"Reflection" 没有解决我的问题。

我想通过字符串调用一个字段,例如:

    string str = "Green";
    Color colorForPurpose = Color.str;

这样当然不行,但是目的要明确。

有人有想法吗?

您可以使用

string str = "Green";
Color colorForPurpose = Color.FromName(str)

有关详细信息,请参阅 this post