自由形式动态 web.config 区域

发布于 2024-11-11 16:32:46 字数 213 浏览 3 评论 0原文

随着 .NET4 的发布,有人创建了一个动态 web.config 元素,它只允许您在配置中输入任何内容,然后您可以通过 dynamic 对象访问它吗?

创建自定义配置部分的工作量似乎毫无理由地超出了顶部。这让我想知道是否有人已经取代了每次轻松创建 5 个以上类来滚动新配置部分的歌舞。

(注意,当我说自由形式时,我显然希望它符合有效的 xml 元素)

With the release of .NET4 has anyone ever created a dynamic web.config element that will just let you type anything you want into the config and then you can access it all off a dynamic object?

The amount of work that goes into creating custom config sections is just over the top for seemingly no reason. This has lead me to wonder if someone has replaced the song and dance of easily needing to create 5+ classes to roll a new config section each and every time.

(Note, when I say free form I would obviously expect it to conform to a valid xml element)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

随波逐流 2024-11-18 16:32:46

如果您只想访问配置文件的 appSettings 部分,您可以继承 DynamicObject 类并重写 TryGetMember 方法:

public class DynamicSettings : DynamicObject {
    public DynamicSettings(NameValueCollection settings) {
        items = settings;
    }

    private readonly NameValueCollection items;

    public override bool TryGetMember(GetMemberBinder binder, out object result) {
        result = items.Get(binder.Name);
        return result != null;
    }
}

然后,假设这是您的 app.config 文件:

<configuration>
  <appSettings>
    <add key="FavoriteNumber" value="3" />
  </appSettings>
</configuration>

...可以像这样访问“FavoriteNumber”设置:

class Program {
    static void Main(string[] args) {
        dynamic settings = new DynamicSettings(ConfigurationManager.AppSettings);
        Console.WriteLine("The value of 'FavoriteNumber' is: " + settings.FavoriteNumber);
    }
}

请注意,尝试访问未定义的密钥会导致 RuntimeBinderException 被扔掉。您可以通过将重写的 TryGetMember 更改为始终返回 true 来防止这种情况,在这种情况下,未定义的属性将仅返回 null

If you just want to access the appSettings section of the configuration file, you can inherit from the DynamicObject class and override the TryGetMember method:

public class DynamicSettings : DynamicObject {
    public DynamicSettings(NameValueCollection settings) {
        items = settings;
    }

    private readonly NameValueCollection items;

    public override bool TryGetMember(GetMemberBinder binder, out object result) {
        result = items.Get(binder.Name);
        return result != null;
    }
}

Then, assuming this is your app.config file:

<configuration>
  <appSettings>
    <add key="FavoriteNumber" value="3" />
  </appSettings>
</configuration>

... the 'FavoriteNumber' setting could be accessed like so:

class Program {
    static void Main(string[] args) {
        dynamic settings = new DynamicSettings(ConfigurationManager.AppSettings);
        Console.WriteLine("The value of 'FavoriteNumber' is: " + settings.FavoriteNumber);
    }
}

Note that attempting to access a key that is undefined results in a RuntimeBinderException being thrown. You could prevent this by changing the overridden TryGetMember to always return true, in which case undefined properties will simply return null.

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