Я потратил несколько недель, пытаясь выяснить это, это дубликат вопроса, который я задавал ранее, но не получил ответа, поэтому я уточняю вопрос здесь.
Я создал собственный класс:
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: не удалось загрузить тип 'mssql_gui.TestConfigSection' из сборки 'System.Configuration, Version = 4.0.0.0, Culture = нейтральный, PublicKeyToken = b03f5f7f11d50a3a'.
Я понимаю, что в типе мне нужно объявить dll для сборки, но я в замешательстве по этому поводу ... потому что в официальных инструкциях от MS говорится о создании нового класса для обработчик:
Создать открытый класс, который наследуется от
Класс System.Configuration.ConfigurationSection.
Добавьте код для определения атрибутов и элементов раздела.
При добавлении класса (по крайней мере, через интерфейс Visual Studio) создается файл .cs, а не файл сборки .dll, так как добавить этот пользовательский класс в файл сборки, чтобы ссылаться на него в <configSections>
часть app.config?