.NET 配置节设计器 - 我的收藏在哪里? [英] .NET Configuration Section Designer - Where is my collection?

查看:15
本文介绍了.NET 配置节设计器 - 我的收藏在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Configuration Section Designer for .NET 来构建一个简单的 ConfigurationElementCollection.似乎一切都构建得很好并且代码是自动生成的,因为子元素在 Intellisense 中可见.

I am using the Configuration Section Designer for .NET to build a simple ConfigurationElementCollection. It appears that everything builds just fine and the code is automatically generated since the sub-elements are visible with Intellisense.

不幸的是,即使我在配置部分向集合中添加了元素,它们在运行时并不存在;它显示集合的 Length 属性为 0 - 即集合是空的,正如您在我的示例 XML 中看到的那样,我已经清楚地将它们放在那里:

Unfortunately, even though I have added elements to the collection in the configuration section, they do not exist at run-time; it shows the collection's Length property to be 0 - i.e. the collection is empty even though, as you can see in my example XML, I have clearly put them there:

<logParserSettings xmlns="LogParser">
  <domainControllers>
    <domainController ID="0" name="Local" serverType="Local" enabled="true"/>
    <domainController ID="1" name="DC1" serverType="WindowsServer2003" enabled="false" />
    <domainController ID="2" name="DC2" serverType="WindowsServer2008" enabled="false" />
  </domainControllers>
</logParserSettings>

杂项

  • 在设计器中,我所拥有的只是您在此处看到的内容,一个 ConfigurationSection、一个 ConfigurationElementCollection 和一个 ConfigurationElement - 没有其他配置元素/部分/组
  • 我不会在运行时添加或删除任何内容
  • 我已尝试生成和不生成 Singleton 属性,但单例实例和设置类的实例似乎都不起作用
  • 对于 ID 属性,将 Is Key 属性设置为 true,对于 Is Required
  • 将所有其他属性标记为 true

    Miscellaneous

    • In the designer, all I have is what you see here, a ConfigurationSection, a ConfigurationElementCollection, and a ConfigurationElement - there are no additional configuration elements/sections/groups
    • I am not adding or removing anything at run-time
    • I have tried both generating and not generating the Singleton property, but neither the singleton instance nor an instance of the settings class seem to work
    • For the ID attribute, the Is Key property is set to true, and all other attributes are marked true for Is Required
    • 有人遇到过这种情况吗?如果是这样,我必须进行哪些更改才能使其按预期工作?

      Has anyone ever run into this? If so, what change do I have to make to get this to work as intended?

      推荐答案

      好吧,我通过将问题具体化到特定元素,使真正的问题严重过度复杂化,结果发现 上甚至没有一个简单的测试属性ConfigurationSection 将返回一个预期值.我也意识到我无法访问 AppSettings,这导致了一个肆虐的线索.

      Well, I grossly overcomplicated the true issue by specifying the problem down to a specific element, when it turns out not even a simple test attribute on the ConfigurationSection would return an expected value. I also realized that I couldn't access AppSettings, which resulted in a raging clue.

      最终原因?该项目被编译为类库.配置文件仅在应用程序级别可用.由于我使用 NUnit 来运行测试,因此测试项目的配置文件覆盖了类库的开发文件.对于没有陷入 ASP.NET 领域的任何开发人员来说,这可能是显而易见的.

      The ultimate cause? The project was compiled as a class library. Config files are only available at the application level. Since I was using NUnit to run the tests, the configuration file from the test project was overriding the development one for the class library. This is probably obvious to any developers who are not stuck in ASP.NET land.

      通过将以下 configSection 添加到测试项目中的 app.config:

      By adding the following configSection to the app.config within the test project:

      <section name="customConfigSection" type="ClassLibraryNamespace.SettingsClass, ClassLibraryNamespace"/>
      

      并填写相应的部分,可以按预期访问数据.

      and filling in the corresponding section, the data could be accessed as expected.

      希望这可以节省一些人的调试时间.

      Hopefully, this will save someone hours of debugging.

      这篇关于.NET 配置节设计器 - 我的收藏在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆