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).
Accessing the values using MySettings.NAME_OF_THAT_THING is no problem, but what if the name is variable?
I tried:
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?
All you need to do is:
String propertyValue = Settings.Default["NAME_OF_THAT_THING"].ToString();
While using reflection will obviously work, it's overkill.
String propertyValue = MySettings.GetType()
.GetProperty("NAME_OF_THAT_THING")
.GetValue(MySettings, null); //replace MySettings with null in GetValue(...) if MySettings is a static class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With