Benefits of .Net's AppSettingsReader vs ConfigurationManager for reading application configuration settings
Is there a substantial difference between the AppSettingsReader开发者_C百科 class and the AppSettings member of the ConfigurationManager class in .Net 3.5?
I'm building out some legacy code and a previous developer used AppSettingsReader.GetValue(), whereas I am more prone to use ConfigurationManager.AppSettings.Get().
Looking at the internals, AppSettingReader seems to be more typesafe, but its usage seems slightly more verbose. If the app settings I'm retrieving are well-known and fairly static, is there any benefit to using AppSettingsReader?
Not really. Internally AppSettingsReader just used the ConfigurationManager.AppSettings.
There is basically just a bit of fluff around checking that it exists, checking that what is going on is ok etc.
One thing is though that it will throw an exception when there is no element in the AppSettings..
// From CTOR
this.map = ConfigurationManager.AppSettings;
....
public object GetValue(string key, Type type)
...
string item = this.map[key];
if (item == null)
{
throw new InvalidOperationException(SR.GetString("AppSettingsReaderNoKey", new object[] { key }));
}
I think that most people will just use ConfigurationManager but the real answer is 'wahtever floats your boat'.
精彩评论