Ошибка при чтении пользовательского файла конфигурации (app.config) - PullRequest
0 голосов
/ 26 апреля 2010

Я делаю пользовательскую конфигурацию в своем приложении 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;
            }

После запуска приложения и нажатия на кнопку я получаю эту ошибку

Не удалось загрузить тип 'UtilityMethods.CountryList' из сборки 'System.Configuration, версия = 2.0.0.0, культура = нейтральная, PublicKeyToken = b03f5f7f11d50a3a'

Пожалуйста, помогите (dotnet framework: 3.5 Язык: C #)

1 Ответ

0 голосов
/ 27 апреля 2010

Я разобрался. Вместо использования

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

Это будет

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

т.е. namespace.classname пространство имен

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...