将自己的configSection添加到App.Config [英] Adding own configSection to App.Config
问题描述
您好。
我尝试在App.Config中添加部分。
I tried to add section in App.Config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="workers" type="MyProject.Setup.Config.WorkerElement, MyProject" />
</configSections>
<workers>
<worker name="Name" type="Operator" />
</workers>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
</startup>
</configuration>
课程:
public class WorkersConfigSection : ConfigurationSection
{
[ConfigurationProperty("workers")]
public WorkersCollection WorkersCollection
{
get => ((WorkersCollection) base["workers"]);
set => base["workers"] = value;
}
}
[ConfigurationCollection(typeof(WorkerElement), AddItemName = "worker")]
public class WorkersCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new WorkerElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((WorkerElement)element).Name;
}
}
public class WorkerElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get => (string)base["name"];
set => base["name"] = value;
}
[ConfigurationProperty("type", IsKey = false, IsRequired = true)]
public WorkerType Type
{
get => (WorkerType)base["type"];
set => base["type"] = value;
}
}
但是在调试时我捕获异常类型 'MyProject.Setup.Config.WorkerElement'未实现'System.Configuration.IConfigurationSectionHandler'。
But on debug I catch exception Type 'MyProject.Setup.Config.WorkerElement' does not implement 'System.Configuration.IConfigurationSectionHandler'.
我哪里弄错了?
推荐答案
嗨 fadeinmad,
>>但是在调试中我抓住了异常类型"MyProject.Setup.Config.WorkerElement"未实现"System.Configuration.IConfigurationSectionHandler"。
请请参考以下代码示例并修改您的应用程序。
Hi fadeinmad,
>>But on debug I catch exception Type 'MyProject.Setup.Config.WorkerElement' does not implement 'System.Configuration.IConfigurationSectionHandler'.
Please refer the following code sample and modify your application.
<configSections>
<section name="WorkersConfigSection" type="WpfApp_temp2.WorkersConfigSection, WpfApp_temp2" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<WorkersConfigSection>
<workers>
<worker name="Name" type="Operator" />
<worker name="Name2" type="Operator2"></worker>
</workers>
</WorkersConfigSection>
private void Button_Click(object sender, RoutedEventArgs e)
{
var connectionManagerDataSection = ConfigurationManager.GetSection(WorkersConfigSection.SectionName) as WorkersConfigSection;
if (connectionManagerDataSection != null)
{
foreach (WorkerElement endpointElement in connectionManagerDataSection.WorkersCollection)
{
var endpoint = new WorkerElement() { Name = endpointElement.Name , Type=endpointElement.Type };
MessageBox.Show("Name: "+endpoint.Name+"; Type: "+ endpoint.Type);
}
}
}
public class WorkersConfigSection : ConfigurationSection
{
public const string SectionName = "WorkersConfigSection";
[ConfigurationProperty("workers")]
[ConfigurationCollection(typeof(WorkersCollection), AddItemName = "worker")]
public WorkersCollection WorkersCollection
{
get => ((WorkersCollection)base["workers"]);
set => base["workers"] = value;
}
}
public class WorkersCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new WorkerElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((WorkerElement)element).Name;
}
}
public class WorkerElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get => (string)base["name"];
set => base["name"] = value;
}
[ConfigurationProperty("type", IsKey = false, IsRequired = true)]
public string Type
{
get => (string)base["type"];
set => base["type"] = value;
}
}
$
此外,如果您可以通过将有用的帖子标记为答案来关闭该帖子,将不胜感激。如果您有新问题,可以开始新主题。
Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. If you have a new question you can start a new thread.
这篇关于将自己的configSection添加到App.Config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!