读取自定义配置文件(app.config)时出错

时间:2010-04-26 11:20:08

标签: c#-3.0

我正在winform应用程序中进行自定义配置。 (它将代表一个国家/地区的名单)

首先是 CountryList类

namespace UtilityMethods
{
    public class CountryList : ConfigurationSection
    {
        public CountryList()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        [ConfigurationProperty("CountryCurrency", IsRequired = true)]
        public Hashtable CountryCurrencies
        {
            get
            {
                return CountryCurrency.GetCountryCurrency();
            }
        }
    }
}

GetCountryCurrency()方法在 CountryCurrency 类中定义为

namespace UtilityMethods
{
    public static class CountryCurrency
    {
        public static Hashtable GetCountryCurrency()
        {
            Hashtable ht = new Hashtable();
            ht.Add("India", "Rupees");
            ht.Add("USA", "Dollar");
            return ht;
        }
    }
}

app.config文件看起来像

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name ="CountryList1" type ="UtilityMethods.CountryList,CountryList,Version=2.0.0.0, 
          Culture=neutral"/>
  </configSections>
  <appSettings />

</configuration>

我从button_click的事件中称之为

try
            {
                CountryList cList  = ConfigurationManager.GetSection("CountryList") as CountryList;
                Hashtable ht = cList.CountryCurrencies;
            }
            catch (Exception ex)
            {
                string h = ex.Message;
            }

运行应用程序并单击按钮后,我收到此错误

无法从程序集'System.Configuration,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'

加载类型'UtilityMethods.CountryList'

请帮助(dotnet框架:3.5语言:C#)

1 个答案:

答案 0 :(得分:0)

我想通了。而不是使用

<section name ="CountryList1" type ="UtilityMethods.CountryList,CountryList,Version=2.0.0.0, Culture=neutral"/> 

将是

<section name ="CountryList1" type ="UtilityMethods.CountryList,UtilityMethods"/> 

即。 namespace.classname,命名空间