如何将自定义 ConfigurationSection 添加到程序集?

How do I add custom ConfigurationSection to Assembly?

我花了几个星期的时间来弄清楚这个问题,这是我之前提出的一个问题的重复,但没有得到答复,所以我在这里提炼这个问题。

我创建了自定义 class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Configuration;

namespace mssql_gui
{
    public class TestConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
        public TestConfigInstanceCollection Instances
        {
            get { return (TestConfigInstanceCollection)this[""]; }
            set { this[""] = value; }
        }
    }

    public class TestConfigInstanceCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new TestConfigInstanceElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((TestConfigInstanceElement)element).Key;
        }
    }

    public class TestConfigInstanceElement : ConfigurationElement
    {
        [ConfigurationProperty("key", IsKey = true, IsRequired = true)]
        public string Key
        {
            get { return (string)base["key"]; }
            set { base["key"] = value; }
        }
        [ConfigurationProperty("value", IsRequired = true)]
        public string Value
        {
            get { return (string)base["value"]; }
            set { base["value"] = value; }
        }
    }
}

我已经实现了:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="testSection" type="mssql_gui.TestConfigSection"/>
  </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
    </startup>
  <appSettings>
    <add key="Data Source" value="localhost\SQLEXPRESS"/>
    <add key="Initial Catalog" value="(empty)"/>
    <add key="Integrated Security" value="SSPI"/>
  </appSettings>
  <testSection>
    <add key ="testKey" value="tesValue"/>
  </testSection>
</configuration>

我尝试访问它,我得到:

An error occurred creating the configuration section handler for testSection: Could not load type 'mssql_gui.TestConfigSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

我知道在类型中,我应该声明一个程序集 dll,但我对此感到困惑...因为在 official instructions by MS 中,它说要创建一个新的 class对于处理程序:

  1. Create a public class that inherits from the System.Configuration.ConfigurationSection class.

  2. Add code to define the section's attributes and elements.

添加 class(至少通过 visual studio 接口)会创建一个 .cs 文件,而不是 .dll 程序集文件,那么如何将自定义 class 添加到一个程序集文件,以便在 app.config?

<configSections> 部分引用它

Ensure that the type attribute of the section element matches the manifest of the assembly (ensure that you specify both the correct namespace and type name).

您需要将程序集的名称(类型依赖的地方)添加到类型属性中:

您将从定义了 TestConfigSection class 的项目中的 AssemblyInfo.cs 中获取程序集的名称。

 <section name="testSection" type="mssql_gui.TestConfigSection, ASSEMBLYNAME"/>

示例假设您的程序集名称 mssql_gui

 <section name="testSection" type="mssql_gui.TestConfigSection, mssql_gui"/>

你是这样读的:

 Configuration config =
 ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 TestConfigSection mySec = (TestConfigSection)config.Sections["testSection"];

在 MSDN 上查看更多详细信息

How to: Create Custom Configuration Sections Using ConfigurationSection

如果我理解正确的话,您在解析 Assembly 的实际内容时遇到了问题,因为您只是创建了 .cs 文件来确定 types 该文件所包含的内容。

程序集(可能不是那么准确的快捷方式)只是您在解决方案中的项目。它将被编译到它的单独程序集中 - 你提到的 .dll - 稍后。 当您将 class 添加到给定项目中的任何 .cs 文件时,编译时它将包含在项目的程序集中。

默认情况下,如果您不为 configSection 提供程序集,那么 App.config 默认为 System.Configuration 程序集 - 这就是您从中得到错误的地方,因为您已经在自己的程序集(== 项目)中声明了您的部分。

右键单击包含 App.config 文件的项目的 Visual Studio,然后选择 Properties 检查其 程序集名称:

然后将此名称添加到您的 App.config 部分声明中。在我的示例中,它是 ConsoleApp1,因此我将相应地将其添加到配置中:

<configSections>
    <section name="testSection" type="mssql_gui.TestConfigSection, ConsoleApp1"/>
</configSections>