需要创建一个也保存类型的动态 ConfigurationSection

发布于 2024-07-06 12:45:39 字数 1113 浏览 10 评论 0原文

我需要创建一个配置部分,它能够在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

第七度阳光i 2024-07-13 12:45:39

Phil Haack 有一篇关于 创建自定义配置部分

Phil Haack has a great article on Creating Custom Configuration Sections

梦里°也失望 2024-07-13 12:45:39

我在 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

留一抹残留的笑 2024-07-13 12:45:39

这就是您在 ASCII 文本文件中获得的全部内容 - 字符串。 :-)

但是,您可以对“值”字符串进行编码以包含类型参数,例如

<key="myParam" value="type, value" />

<key="payRate" value="money,85.79"/>

然后让您的应用程序进行转换...

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:

<key="myParam" value="type, value" />

for example:

<key="payRate" value="money,85.79"/>

then have your app do the conversion ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文