需要创建一个也保存类型的动态 ConfigurationSection
我需要创建一个配置部分,它能够在 app.config 文件中存储键值对,并且无论其类型如何,都可以在运行时添加键值对。 该值保持其原始类型也很重要。 我需要扩展以下接口
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;
}
}
I need to create a configuration section, that is able to store key-value pairs in an app.config file and the key-value pairs can be added runtime regardless of their type. It is also important that the value keeps its original type. I need to extend the following interface
public interface IPreferencesBackend
{
bool TryGet<T>(string key, out T value);
bool TrySet<T>(string key, T value);
}
At runtime, I can say something like:
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);
I tried with System.Configuration.Configuration.AppSettings, but the problem with that that it is storing the key-value pairs in a string array.
What I need is to have an implementation of System.Configuration.ConfigurationSection, where I can control the how the individual setting is serialized. I noticed that the settings generated by Visual Studio kind of do this. It is using reflection to create all the setting keys. what I need is to do this runtime and dynamically.
[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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Phil Haack 有一篇关于 创建自定义配置部分
Phil Haack has a great article on Creating Custom Configuration Sections
我在 codeproject.com 上发现了两篇很棒的文章,它们非常详细地解释了这些问题。
揭开 .NET 2.0 配置之谜
http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx
应用的用户设置
http://www.codeproject.com/KB/dotnet/user_settings.aspx?display=PrintAll&fid=1286606&df=90&mpp=25&噪音=3&sort=Position&view=Quick&select=2647446&fr=26
I found two great articles on codeproject.com that are explaining these issues in great detail.
Unraveling the Mysteries of .NET 2.0 Configuration
http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx
User Settings Applied
http://www.codeproject.com/KB/dotnet/user_settings.aspx?display=PrintAll&fid=1286606&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2647446&fr=26
这就是您在 ASCII 文本文件中获得的全部内容 - 字符串。 :-)
但是,您可以对“值”字符串进行编码以包含类型参数,例如
:
然后让您的应用程序进行转换...
That's all you get in a an ASCII text file - strings. :-)
However, you can encode the "value" strings to include a type parameter like:
for example:
then have your app do the conversion ...