简体   繁体   English

ConfigurationManager.GetSection跳过重复项

[英]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 . 这允许您将内部配置变量表示为集合,并设置DefaultValueIsRequired等属性。

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.

相关问题 ConfigurationManager.GetSection()函数 - ConfigurationManager.GetSection() function ConfigurationManager.GetSection 返回 null - ConfigurationManager.GetSection returns null ReadOnlyNameValueCollection(从ConfigurationManager.GetSection读取) - ReadOnlyNameValueCollection (reading from ConfigurationManager.GetSection) ConfigurationSection ConfigurationManager.GetSection()始终返回null - ConfigurationSection ConfigurationManager.GetSection() always returns null ConfigurationManager.GetSection和Configuration.GetSection有什么区别? - What is Difference between ConfigurationManager.GetSection and Configuration.GetSection? ConfigurationManager.GetSection始终为对象提供默认值 - ConfigurationManager.GetSection always gives object with default values ConfigurationManager.GetSection不返回自定义部分 - ConfigurationManager.GetSection doesn't return custom section 如何在.Net Core 2.2中正确使用ConfigurationManager.GetSection() - How to use properly ConfigurationManager.GetSection() in .Net Core 2.2 为什么ConfigurationManager.GetSection“ system.webServer / handlers”不可用? - Why is the ConfigurationManager.GetSection “system.webServer/handlers” not available? 在类库的 App.config 中使用 ConfigurationManager.GetSection - Using ConfigurationManager.GetSection in a Class Library's App.config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM