自由形式动态 web.config 区域
随着 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想访问配置文件的
appSettings
部分,您可以继承DynamicObject
类并重写TryGetMember
方法:然后,假设这是您的
app.config
文件:...可以像这样访问“FavoriteNumber”设置:
请注意,尝试访问未定义的密钥会导致
RuntimeBinderException
被扔掉。您可以通过将重写的TryGetMember
更改为始终返回true
来防止这种情况,在这种情况下,未定义的属性将仅返回null
。If you just want to access the
appSettings
section of the configuration file, you can inherit from theDynamicObject
class and override theTryGetMember
method:Then, assuming this is your
app.config
file:... the 'FavoriteNumber' setting could be accessed like so:
Note that attempting to access a key that is undefined results in a
RuntimeBinderException
being thrown. You could prevent this by changing the overriddenTryGetMember
to always returntrue
, in which case undefined properties will simply returnnull
.