从设置中获取动态属性

从设置中获取动态属性

问题描述:

我的 AppConfig 中存储了一些属性,现在我想动态访问它们(例如在循环或函数中).

I've got a few properties stored in my AppConfig and now I want to access them dynamically (e.g. in a loop or function).

使用 MySettings.NAME_OF_THAT_THING 访问值没有问题,但如果名称是可变的怎么办?

Accessing the values using MySettings.NAME_OF_THAT_THING is no problem, but what if the name is variable?

我试过了:

String propertyValue = MySettings.GetType().GetProperty("NAME_OF_THAT_THING").ToString();

但我得到的唯一信息是房产的名称.我该怎么做?

But the only thing I got back is the name of the property. How can I do this?

String propertyValue = MySettings.GetType()
.GetProperty("NAME_OF_THAT_THING")
.GetValue(MySettings, null); //replace MySettings with null in GetValue(...) if MySettings is  a static class