我需要创建一个配置节,它可以将键值对存储在app.config文件中,并且可以将键值对添加到运行时,而不管它们是类型。价值保持其原始类型也很重要。我需要延长如下界面需要创建一个动态ConfigurationSection,其中也包含类型
public interface IPreferencesBackend
{
bool TryGet<T>(string key, out T value);
bool TrySet<T>(string key, T value);
}
在运行时,我可以这样说:
My.Foo.Data data = new My.Foo.Data("blabla");
Pref pref = new Preferences();
pref.TrySet("foo.data", data);
pref.Save();
My.Foo.Data date = new My.Foo.Data();
pref.TryGet("foo.data", out data);
我System.Configuration.Configuration.AppSettings试过,但这个问题,它是存储字符串数组中的键值对。
我需要的是有一个System.Configuration.ConfigurationSection的实现,在那里我可以控制如何个别设置序列化。我注意到由Visual Studio生成的设置会这样做。它使用反射来创建所有设置键。我需要的是做这个运行时间和动态。
[System.Configuration.UserScopedSettingAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.Configuration.DefaultSettingValueAttribute("2008-09-24")]
public global::System.DateTime DateTime {
get {
return ((global::System.DateTime)(this["DateTime"]));
}
set {
this["DateTime"] = value;
}
}