本文介绍了从设置中获取动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 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

这篇关于从设置中获取动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 03:22