ConfigurationManager.GetSection 跳过重复项

发布于 2024-11-03 18:51:57 字数 591 浏览 2 评论 0原文

长话短说,.Net ConfigurationManager.GetSection 方法会跳过重复项,我正在寻找处理此问题的最佳实践。我有一个配置文件,其中包含如下内容:

   <views>
     <view name="BusinessUnitsView" Desc="desc1"/>
     <view name="BusinessUnitsView" Desc="desc2"/>
   </views>

我有一个配置加载到的对象图,其中集合派生自我们的 ConfigurationElementCollection 的派生版本。加载上述内容后,图中的视图集合中只有一项 - 我的理解是,这只是 ConfigurationManager 处理重复项的方式。最后一个具有相同密钥的项目获胜。

我可以在使用 BaseAdd(element, true) 找到重复项时引发异常。但是,如果可能的话,我希望对象完全加载重复项,因为配置是在服务层中读取的,并且我希望检测并处理服务消费端的问题。

我是否可以选择修改添加到集合工作的方式?

Long story short, the .Net ConfigurationManager.GetSection method skips duplicates, and I'm looking for a best practice for handling this. 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. 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. The last item with the same key wins.

I could throw an exception on a duplicate being found by using 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.

Do I have any options for modifying the way adds to the collection work?

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

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

发布评论

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

评论(2

浮光之海 2024-11-10 18:51:57

您需要创建自己的ConfigurationSection。请参阅此处这里(第二个链接的方法已被弃用,但它可能仍然会脱落一些轻)

这允许您将内部配置变量表示为集合,并设置诸如 DefaultValueIsRequired 之类的属性。

You will need to create your own 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.

终遇你 2024-11-10 18:51:57

也许您只想迭代一个列表,但配置的主要思想是您可以

var view = SomeConfigClass["BusinessUnitsView"];

只允许一个答案。

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.

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