本文介绍了从设置获取动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在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
这篇关于从设置获取动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!