【发布时间】:2013-12-05 20:26:34
【问题描述】:
我想写一个带两个参数的方法:
- 字符串 [PropertyName],
- unknown [new propertyValue] --- 可以是字符串、int 或 bool
我正在使用它来更新 web.config 应用设置键中的键值。所以,如果我想更新设置中的字符串值,例如GlobalEmail,我可以这样使用方法:
UpdateSettingsKeyToNewValue("GlobalEmail", "newAddress@outlook.com");
或者我可以像这样更新开关:
UpdateSettingsKeyToNewValue("IsUserSet", false);
我该怎么做呢?目前的代码基本如下,但不知道如何类型检查,然后正确转换,然后应用新值。
public void UpdateKeyValue(string propertyName, Object newValue)
{
Type genericType = typeof(newValue["newValue"]); //not sure how to get here....
Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
config.AppSettings.Settings.Remove(propertyName);
config.AppSettings.Settings.Add(propertyName, newValue);
config.Save();
}
提前致谢!
【问题讨论】:
标签: c# generics web-config