c# - 错误读取配置 : Could not load type . .. 来自程序集 'System.Configuration

标签 c#

我很茫然...调用时出错

 var mySettings = ListOfOrgs.GetSettings();

无法从程序集“System.Configuration,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“DetailedFaxByMonthByOrg.ListOfOrgsSection”。":"DetailedFaxByMonthByOrg.ListOfOrgsSection"

有人可以帮忙吗?

配置:

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
          <section name="ListOfOrgs" type="DetailedFaxByMonthByOrg.ListOfOrgsSection"/>

      </configSections>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>


      <ListOfOrgs>
        <Settings>
          <add name="myComp" userIds="12345,123475" GBuserIds="99999"></add>
          <add name="myComp2" userIds=" 58795,25362" GBuserIds="254300, 956482"></add>
        </Settings>
      </ListOfOrgs>
    </configuration>

代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DetailedFaxByMonthByOrg
{
    public class ListOfOrgs
    {
        public static ListOfOrgsSection _Config = ConfigurationManager.GetSection("ListOfOrgs") as ListOfOrgsSection;
        public static SettingsElementCollection GetSettings()
        {
            return _Config.Settings;
        }
    }
    public class ListOfOrgsSection : ConfigurationSection
    {
        [ConfigurationProperty("Settings")]
        public SettingsElementCollection Settings
        {
            get { return (SettingsElementCollection)this["Settings"]; }
        }
    }

    [ConfigurationCollection(typeof(OrgElement))]
    public class SettingsElementCollection : ConfigurationElementCollection
    {
        public OrgElement this[int index]
        {
            get { return (OrgElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);

                BaseAdd(index, value);
            }
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new OrgElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((OrgElement)element).Name;
        }
    }
    public class OrgElement : ConfigurationElement
    {
        public OrgElement() { }

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

        [ConfigurationProperty("userIds", DefaultValue = "", IsRequired = true)]
        public string userIds
        {
            get { return (string)this["userIds"]; }
            set { this["userIds"] = value; }
        }

        [ConfigurationProperty("GBuserIds", DefaultValue = "", IsRequired = true)]
        public string GBuserIds
        {
            get { return (string)this["GBuserIds"]; }
            set { this["GBuserIds"] = value; }
        }

    }
}

最佳答案

根据 documentation :

<section 
   name="section name"
   type="configuration section handler class, assembly file name, version, culture, public key token"
 .....
 />

将您的 configSection 定义更新为

<section name="ListOfOrgs" type="DetailedFaxByMonthByOrg.ListOfOrgsSection, DetailedFaxByMonthByOrg">

它正在寻找错误的程序集

关于c# - 错误读取配置 : Could not load type . .. 来自程序集 'System.Configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22992923/

相关文章:

c# - 如何简化那部分代码?

c# - 即使在 ".ToList()"调用之后,递归中的 yield 返回也不会返回值

c# - 如何对 IEnumerable<string> 进行排序

c# - 将对象流二进制(反)序列化为 1 个文件

c# - 这个 if 语句会导致不好的事情发生吗?

c# - EmguCV 3.0 中的 CvInvoke.ConvexityDefects()

c# - 对数组中的 BinarySearch 感到困惑

c# - 为什么 XmlDictionary.Add() 返回 XmlDictionaryString?

c# - 具有 linq 查询和存储表达式错误的 Entity Framework

c# 连接到本地 MDF 数据库文件