25

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

我收到错误消息:

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

我完全被难住了,如果有人能帮助我,我会非常感激。

4

2 回答 2

51

在行中:

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

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

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

ProcessesConfigurationSection

(注意流程与流程)

于 2013-09-30T13:20:26.523 回答
3

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

type="Configuration.ProcessConfigurationSection , Configuration"

也会产生这个错误。

于 2015-10-26T13:49:21.507 回答