2010-04-26 52 views
0

我在我的winform應用程序中進行自定義配置。 (它將代表一個國家-corrency列表)讀取自定義配置文件(app.config)時出錯

首先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; 
     } 
    } 
} 

該應用程序.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; 
      } 

在運行應用程序並點擊按鈕,我從收到此錯誤

未能加載類型「UtilityMethods.CountryList」程序集'System.Configuration,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'

請幫忙(dotnet framework:3.5語言:C#)

回答

0

我想使用

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

的out.Instead這將是

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

即namespace.classname,命名空間

相關問題