【问题标题】:How do I add custom ConfigurationSection to Assembly?如何将自定义 ConfigurationSection 添加到程序集?
【发布时间】:2018-07-02 07:06:51
【问题描述】:

我花了几个星期试图弄清楚这一点,这是我之前提出的一个问题的重复,但没有得到答复,所以我在这里完善这个问题。

我创建了一个自定义类:

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,但我对此感到困惑......因为在 official instructions by MS 中,它说要为处理程序创建一个新类:

  1. 创建一个继承自 System.Configuration.ConfigurationSection 类。

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

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

【问题讨论】:

标签: c# .net app-config


【解决方案1】:

确保部分元素的类型属性与 程序集的清单(确保您指定了正确的 命名空间和类型名称)。

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

您将从定义了 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 上查看更多详细信息

How to: Create Custom Configuration Sections Using ConfigurationSection

【讨论】:

  • 谢谢,是的,我试过 mssql_gui,但它产生了类似的错误......我遇到的问题是程序集名称本身有一个空格而不是下划线,但也许你可以帮助我解决与您的答案相关的事情。当我使用你的方法时,我得到“CS0122 'ConfigurationElement.this[ConfigurationProperty]' is inaccessible due to its protection level”
  • 你究竟是从哪里得到错误的?这意味着您要使用的类/属性不可访问。
  • 所以configurationSection类仍然定义如上(在原始问题中)。在我要实现它的类中,我有以下两个语句: mssql_gui.TestConfigSection s = ConfigurationManager.GetSection("testSection") as mssql_gui.TestConfigSection;和 txtCatalog.Text = s["testKey"].Value;错误发生在第二条语句中
  • 你想要的东西是不可能的,你必须使用类似的东西(需要使用 System.Linq;): txtCatalog.Text = s.Instances.Cast().FirstOrDefault(kv= >kv.Key == "testKey")?.Value;但是,也请看一下 NameValueSectionHandler-Class!
【解决方案2】:

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

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

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

在 Visual Studio 中右键单击包含 App.config 文件的项目并选择 Properties 以检查其程序集名称

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

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

【讨论】:

  • 谢谢!问题是程序集名称实际上是“mssql gui”(带空格),我试过 mssql_gui,因为那是生成的命名空间,所以问题实际上只是空格字符。感谢您告诉我在哪里可以找到它!
猜你喜欢
  • 2011-12-22
  • 2016-09-01
  • 2011-04-19
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
相关资源
最近更新 更多