[英]ConfigurationManager.GetSection Skips Duplicates
Long story short, the .Net ConfigurationManager.GetSection
method skips duplicates, and I'm looking for a best practice for handling this. 简而言之,.Net
ConfigurationManager.GetSection
方法跳过重复项,我正在寻找处理此问题的最佳实践。 I have a config file that includes something like this: 我有一个包含这样的配置文件:
<views>
<view name="BusinessUnitsView" Desc="desc1"/>
<view name="BusinessUnitsView" Desc="desc2"/>
</views>
I have a graph of objects the config loads onto where collections derive from our derived version of ConfigurationElementCollection
. 我有一个配置加载到我们派生的
ConfigurationElementCollection
派生集合的对象图。 The views collection in the graph only has one item in it after loading the above--my understanding is that this is simply the way the ConfigurationManager
handled duplicates. 在加载上述内容后,图中的视图集合中只有一个项目 - 我的理解是这只是
ConfigurationManager
处理重复项的方式。 The last item with the same key wins. 具有相同键的最后一项获胜。
I could throw an exception on a duplicate being found by using BaseAdd(element, true). 我可以使用BaseAdd(element,true)在找到的副本上抛出异常。 However, if possible I'd like to get the object completely loaded WITH duplicates, as the config gets read in a service layer, and I'd like to detect and deal with the problem on the consuming side of the service.
但是,如果可能的话,我想让对象完全加载WITH duplicatelic,因为配置在服务层读取,我想检测并处理服务的消费方面的问题。
Do I have any options for modifying the way adds to the collection work? 我是否有任何选项可以修改添加到收集工作的方式?
You will need to create your own ConfigurationSection
. 您需要创建自己的
ConfigurationSection
。 See here or here (the second link's method has been deprecated, but it may still shed some light) 看到这里或这里 (第二个链接的方法已被弃用,但它可能仍然有些亮点)
This allows you to represent internal configuration variables as collections, and set properties like DefaultValue
and IsRequired
. 这允许您将内部配置变量表示为集合,并设置
DefaultValue
和IsRequired
等属性。
Maybe you only want to iterate over a list but the main idea of the config is that you can do 也许你只想迭代列表,但配置的主要思想是你可以做到的
var view = SomeConfigClass["BusinessUnitsView"];
That only allows one answer. 这只允许一个答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.