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

3

我已经花了几周的时间来尝试解决这个问题,这是我之前提出的一个问题的重复,但没有得到回应,所以我在这里进行了问题的精炼。

我创建了一个自定义类:

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>

我尝试访问它时出现了以下错误:

创建 testSection 的配置节处理程序时发生错误:无法从程序集“System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”中加载类型“mssql_gui.TestConfigSection”。

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

  1. 创建一个公共类,该类继承自 System.Configuration.ConfigurationSection 类。

  2. 添加代码以定义部分的属性和元素。

添加该类(至少通过 Visual Studio 界面)会创建一个 .cs 文件,而不是 .dll 程序集文件,那么我该如何将该自定义类添加到程序集文件中,以便在 app.config 的 <configSections> 部分中引用它?


可能是在C#中app.config中的自定义部分引发错误的重复问题。 - codeteq
2个回答

4
确保 section 元素的 type 属性与程序集清单匹配(确保您同时指定正确的命名空间和类型名称)。
您需要将依赖该类型的程序集名称添加到 type 属性中:
您可以从定义 TestConfigSection 类的项目中的 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,了解如何使用ConfigurationSection创建自定义配置节。 如何:使用 ConfigurationSection 创建自定义配置节

谢谢,是的,我已经尝试过mssql_gui,但它生成了类似的错误...我遇到的问题是程序集名称本身有一个空格而不是下划线,但也许你可以帮我解决一些与你的答案相关的问题。当我使用你的方法时,我得到了“CS0122 'ConfigurationElement.this[ConfigurationProperty]'由于其保护级别而无法访问”的错误。 - Tara
你在哪里得到这个错误?这意味着你想要使用的类/属性是不可访问的。 - codeteq
因此,configurationSection类仍然定义如上(在原始问题中)。在我想要实现它的类内部,我有以下两个语句:mssql_gui.TestConfigSection s = ConfigurationManager.GetSection("testSection") as mssql_gui.TestConfigSection;txtCatalog.Text = s["testKey"].Value;错误发生在第二个语句中。 - Tara
1
你想要的不可能实现,你必须使用类似以下的方法 (需要引用System.Linq;): txtCatalog.Text = s.Instances.Cast<TestConfigInstanceElement>().FirstOrDefault(kv=>kv.Key == "testKey")?.Value; 但是,也可以查看NameValueSectionHandler-Class类! - codeteq

4
如果我理解正确,您在解决实际上您的“Assembly”是什么的问题,因为您只创建了确定该文件包含的“types”的“.cs”文件。
Assembly(可能不太准确)就是您在解决方案中拥有的项目。它将编译为其单独的程序集 - 您提到的.dll。当您将类添加到给定项目中的任何“.cs”文件中时,它将包含在项目的程序集中。
默认情况下,如果您不提供应该找到其对应类型的“configSection”的程序集,“App.config”会默认使用“System.Configuration”程序集 - 这就是您遇到错误的原因,因为您已在自己的程序集(==项目)中声明了该部分。
在Visual Studio中右键单击包含“App.config”文件的项目,然后选择“属性”以检查其“Assembly name”。

enter image description here

然后将此名称添加到您的App.config部分声明中。在我的示例中,它是ConsoleApp1,因此我会相应地将其添加到配置中:
<configSections>
    <section name="testSection" type="mssql_gui.TestConfigSection, ConsoleApp1"/>
</configSections>

谢谢!问题在于程序集名称实际上是“mssql gui”(带有一个空格),我之前尝试过mssql_gui,因为那是生成的命名空间,所以问题实际上只是空格字符。感谢您指导我去找它! - Tara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接