Get dynamic property from Settings

前端 未结 4 923
闹比i
闹比i 2021-01-04 21:01

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_

相关标签:
4条回答
  • 2021-01-04 21:47

    All you need to do is:

    String propertyValue = Settings.Default["NAME_OF_THAT_THING"].ToString();
    

    While using reflection will obviously work, it's overkill.

    0 讨论(0)
  • 2021-01-04 21:55
    String propertyValue = MySettings.GetType()
    .GetProperty("NAME_OF_THAT_THING")
    .GetValue(MySettings, null); //replace MySettings with null in GetValue(...) if MySettings is  a static class
    
    0 讨论(0)
  • 2021-01-04 21:55

    Have you tried using the ConfigurationManager.AppSettings property? You should be able to get your setting via the following code:

    String propertyValue = ConfigurationManager.AppSettings["NAME_OF_THAT_THING"];
    

    The MSDN article for ConfigurationManager.AppSettings also includes an example for looping through all of the entries in AppSettings by index, rather than name.

    0 讨论(0)
  • 2021-01-04 21:58

    answer to the orginal poster question would be like: MySettings[NAME_OF_THAT_THINGmysettings] does the job like previous post

    however for those people looking for answer to using the builtin settings in their windows app: myAppDefaultNameSpace.Properties.Settings.Default[NAME_OF_THAT_THINGmysettings] is the way to go

    0 讨论(0)
提交回复
热议问题