2014-04-10 33 views
4

我很茫然......得到一个错误‘DetailedFaxByMonthByOrg.ListOfOrgsSection’从程序集加载类型...装配 'System.Configuration,版本= 4.0.0.0,文化=中性公钥= b03f5f7f11d50a3a'。 “:” DetailedFaxByMonthByOrg.ListOfOrgsSection“}错误读取配置:无法从“呼叫</p> <pre><code>var mySettings = ListOfOrgs.GetSettings(); </code></pre> <p>未能加载类型时System.Configuration

有人可以帮

配置:

<?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; } 
     } 

    } 
} 
+0

什么VERS .net是否是您的项目定位?它是否低于4.0? – JBurlison

回答

18

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"> 

它找错了装配

+0

非常感谢!我明显错过了! – user3520042

+0

没问题!我将不胜感激,如果这个答案解决了你的问题,那么你将它标记为我所做的正确答案 – Jonesopolis

+0

,谢谢! – user3520042