gpt4 book ai didi

c# - ConfigurationManager.GetSection 给出错误 "Could not load type....from assembly..."

转载 作者:IT王子 更新时间:2023-10-29 04:23:32 25 4
gpt4 key购买 nike

我的app.config文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ProcessConfiguration" type="Configuration.ProcessConfigurationSection, Configuration" />
</configSections>
<ProcessConfiguration>
<processes>
<process name="Process1" />
</processes>
</ProcessConfiguration>
</configuration>

我有以下(单独的)类来获取配置:

namespace Configuration
{
using System.Configuration;

public class ProcessesConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("processes", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(ProcessCollection))]
public ProcessCollection Processes
{
get
{
return (ProcessCollection)base["processes"];
}
}
}
}

namespace Configuration
{
using System.Configuration;

public class ProcessCollection : ConfigurationElementCollection
{
public ProcessConfig this[int index]
{
get
{
return (ProcessConfig)BaseGet(index);
}

set
{
BaseAdd(index, value);
}
}

protected override object GetElementKey(ConfigurationElement element)
{
return ((ProcessConfig)element).Name;
}

protected override ConfigurationElement CreateNewElement()
{
return new ProcessConfig();
}
}
}

namespace Configuration
{
using System.Configuration;

public class ProcessConfig : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get
{
return (string)this["name"];
}
set
{
this["name"] = value;
}
}
}
}

但是当我点击这行代码时:

var processConfigurationSection = ConfigurationManager.GetSection("ProcessConfiguration") as ProcessesConfigurationSection;

我收到错误信息:

An error occurred creating the configuration section handler for ProcessConfiguration: Could not load type 'Configuration.ProcessConfigurationSection' from assembly 'Configuration'.

我完全被难住了,如果有人能帮助我,我将不胜感激。

最佳答案

行内:

<section name="ProcessConfiguration" type="Configuration.ProcessConfigurationSection, Configuration" />

名称“Configuration”应该指的是您正在构建的 DLL,请尝试检查并在需要时进行更正。

另外我认为你可能有错字,在你的代码中类型名称是:

ProcessesConfigurationSection

(注意流程与流程)

关于c# - ConfigurationManager.GetSection 给出错误 "Could not load type....from assembly...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19095215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com