C# ConfigurationManager.GetSection 给出错误“无法加载类型......从程序集......”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19095215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:04:50  来源:igfitidea点击:

ConfigurationManager.GetSection Gives Error "Could not load type....from assembly..."

c#app-configsystem.configuration

提问by RichardB

My app.configfile is as follows:

我的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>

I have the following (separate) classes to get the 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;
            }
        }
    }
}

However when I hit this line of code:

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

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

I get the error which states:

我收到错误说明:

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

为 ProcessConfiguration 创建配置节处理程序时出错:无法从程序集“Configuration”加载类型“Configuration.ProcessConfigurationSection”。

I'm completely stumped, if anyone can help me out I'd really appreciate it.

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

采纳答案by Justin Harvey

In the line:

在行中:

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

The name 'Configuration' should refer to the DLL that you re building, please try checking this and correct if needed.

名称“配置”应指代您正在构建的 DLL,请尝试检查并在需要时更正。

Also I think you may have a typo, in your code the type name is:

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

ProcessesConfigurationSection

(Note the Processes vs Process)

(注意过程与过程)

回答by AAAA

This error has been raised because the invoking assembly couldn't load the class type in the specified assembly. This error can be caused by a spaceafter the type name (which has happened to me), for example the following config section

已引发此错误,因为调用程序集无法加载指定程序集中的类类型。这个错误可能是由类型名称后面的空格引起的(这发生在我身上),例如下面的配置部分

type="Configuration.ProcessConfigurationSection , Configuration"

will generate this error too.

也会产生这个错误。