【问题标题】:.NET - Referencing Custom Configuration Elements in App.Config.NET - 在 App.Config 中引用自定义配置元素
【发布时间】:2018-10-01 06:13:37
【问题描述】:

我有一个 .NET 4.7 应用程序。在此应用程序中,我有几个要在 App.config 文件中使用的自定义配置元素。出于这个原因,我在我的项目中定义了几个与配置相关的类。该项目编译,其结构如下:

./
  /ConfigurationItems
    Children
    Child
  App.config
  Program.cs

在我的 App.config 文件中,我有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="Children" type="MyOrg.MyApp.ConfigurationItems.Children, MyOrg.MyApp.exe" />
  </configSections>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
  </startup>  

  <Children>
    <Child Name="Joe" />
    <Child Name="Suzy" />
  </Children>  
</configuration>

当我运行这个应用程序时,我得到以下行:

  var children = ConfigurationManager.GetSection("Children") as MyOrg.MyApp.ConfigurationItems.Children;

此时,我收到以下异常:

System.Configuration.ConfigurationErrorsException: 'An error occurred creating the configuration section handler for Children: Could not load file or assembly 'MyOrg.MyApp.exe' or one of its dependencies. The system cannot find the file specified.'

FileNotFoundException: Could not load file or assembly 'MyOrg.MyApp.exe' or one of its dependencies. The system cannot find the file specified.

我做错了什么?如何使用项目中定义的自定义配置元素?

谢谢

【问题讨论】:

    标签: c# configuration app-config configurationsection


    【解决方案1】:

    &lt;section&gt; 元素中删除type 属性中的“.exe”文件扩展名:

      <configSections>
        <section name="Children" type="MyOrg.MyApp.ConfigurationItems.Children, MyOrg.MyApp" />
      </configSections>
    

    【讨论】:

    • @Zach Templeton:请您也接受和/或支持这个答案吗?谢谢!
    猜你喜欢
    • 2020-08-30
    • 1970-01-01
    • 2016-10-01
    • 2021-09-18
    • 2011-07-31
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 2012-08-11
    相关资源
    最近更新 更多