简体   繁体   English

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

[英]ConfigurationManager.GetSection Gives Error “Could not load type…from assembly…”

My app.config file 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. 我很沮丧,如果有人可以帮助我,我将非常感激。

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) (请注意流程与流程)

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 space after the type name (which has happened to me), for example the following config section 此错误可能是由类型名称后面的空格 (这在我身上发生)引起的,例如以下配置部分

type="Configuration.ProcessConfigurationSection , Configuration"

will generate this error too. 也会产生这个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#ConfigurationManager.GetSection无法加载文件或程序集 - C# ConfigurationManager.GetSection could not load file or assembly ReadOnlyNameValueCollection(从ConfigurationManager.GetSection读取) - ReadOnlyNameValueCollection (reading from ConfigurationManager.GetSection) ConfigurationManager.GetSection()函数 - ConfigurationManager.GetSection() function ConfigurationManager.GetSection始终为对象提供默认值 - ConfigurationManager.GetSection always gives object with default values ConfigurationManager.GetSection跳过重复项 - ConfigurationManager.GetSection Skips Duplicates ConfigurationManager.GetSection 返回 null - ConfigurationManager.GetSection returns null ConfigurationManager.GetSection 不继承自 System.Configuration.IConfigurationSectionHandler - ConfigurationManager.GetSection does not inherit from System.Configuration.IConfigurationSectionHandler ConfigurationSection ConfigurationManager.GetSection()始终返回null - ConfigurationSection ConfigurationManager.GetSection() always returns null ConfigurationManager.GetSection和Configuration.GetSection有什么区别? - What is Difference between ConfigurationManager.GetSection and Configuration.GetSection? ConfigurationManager.GetSection不返回自定义部分 - ConfigurationManager.GetSection doesn't return custom section
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM