C#App.Config - 没有获取课程集合的值

时间:2018-05-13 02:03:00

标签: c#

我正在尝试将配置文件中的值读入我的程序。我正在尝试一个我在http://www.bardev.com/2013/11/17/kickstart-c-custom-configuration/在线找到的例子。

我设置配置和类的方式与我在网站上看到的方式相同。我可以获取学校名称,地址,城市和州的值,但我无法取回Settings.Courses值,并且在运行代码时也会收到以下错误:

异常未处理

  

在SchoolBlog.exe中发生'System.TypeInitializationException'。附加信息:'SchoolBlog.SchoolConfig'的类型初始化程序引发了异常。

内部异常(问题的关键部分)

  

ConfigurationErrorsException:无法识别的元素'courses'。

配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="school" type="SchoolBlog.SchoolConfig, SchoolBlog" />
  </configSections>

  <school name="Manchester High">
    <address street="123 Main Street" city="Mandeville" state="Manchester"/>
    <courses>
      <course title="Math"></course>
      <course title="English" instructor="Thompson"></course>
    </courses>
  </school>            
</configuration>

public class SchoolConfig : ConfigurationSection
{
   private static SchoolConfig _schoolConfig = (SchoolConfig)ConfigurationManager.GetSection("school");
   public static SchoolConfig Settings { get { return _schoolConfig; } }


   [ConfigurationProperty("name")]
   public string Name { get { return (string)base["name"]; } }


   [ConfigurationProperty("address")]
   public AddressElement Address { get { return (AddressElement)base["address"]; } }
}

public class AddressElement : ConfigurationElement
{
   [ConfigurationProperty("street")]
   public string Street { get { return (string)base["street"]; } }

   [ConfigurationProperty("city")]
   public string City { get { return (string)base["city"]; } }

   [ConfigurationProperty("state")]
   public string State { get { return (string)base["state"]; } }
}

public class CourseElement : ConfigurationElement
{
   [ConfigurationProperty("title", IsRequired = true)]
   public string Title { get { return (string)base["title"]; } }

   [ConfigurationProperty("instructor", IsRequired = false)]
   public string Instructor { get { return (string)base["instructor"]; } }

   [ConfigurationProperty("courses")]
   public CourseElementCollection Courses { get { return (CourseElementCollection)base["courses"]; } }
}

[ConfigurationCollection(typeof(CourseElement), AddItemName = "course", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class CourseElementCollection : ConfigurationElementCollection
{
   public ConfigurationElementCollectionType CollectionType
   { get { return ConfigurationElementCollectionType.BasicMap; } }

   protected override string ElementName
   {
      get
      {
         //return base.ElementName;
         return "course";
      }
   }

   protected override ConfigurationElement CreateNewElement()
   {
      return new CourseElement();
   }

   protected override object GetElementKey(ConfigurationElement element)
   {
      return (element as CourseElement).Title;
   }

   public CourseElement this[int index]
   {
      get { return (CourseElement)base.BaseGet(index); }
      set
      {
         if (base.BaseGet(index) != null)
         {
            base.BaseRemoveAt(index);
         }
         base.BaseAdd(index, value);
      }
   }

   public CourseElement this[string title]
   { get { return (CourseElement)base.BaseGet(title); } }
}

主要

static void Main(string[] args)
{

   //int coursesCount = SchoolConfig.Settings;
   string schoolName = SchoolConfig.Settings.Name;
   string schoolStreet = SchoolConfig.Settings.Address.Street;
   string schoolCity = SchoolConfig.Settings.Address.City;
   string schoolState = SchoolConfig.Settings.Address.State;

   Console.WriteLine("School Name: {0} ", schoolName);
   Console.WriteLine("School Address: {0} {1} {2} ", schoolStreet, schoolCity, schoolState);
   //Console.WriteLine("Course Count: {0} ", coursesCount);

   Console.WriteLine("\r\nDone");
   Console.ReadLine();
}

CourseElement类继承自ConfigurationElementCouseElement类与我们之前创建的其他Element类非常相似。尽管我不会详细介绍CourseElement类,但请注意每个IsRequired属性的ConfigurationProperty属性。一个设置为true,另一个设置为false。

1 个答案:

答案 0 :(得分:0)

SchoolConfig的类缺少Courses的属性。将类更新为以下内容,它应该可以正常工作。

public class SchoolConfig : ConfigurationSection
{
    public static SchoolConfig Settings { get; } = (SchoolConfig)ConfigurationManager.GetSection("school");

    [ConfigurationProperty("name")]
    public string Name { get { return (string)base["name"]; } }

    [ConfigurationProperty("address")]
    public AddressElement Address { get { return (AddressElement)base["address"]; } }

    [ConfigurationProperty("courses")]
    public CourseElementCollection Courses { get { return base["courses"] as CourseElementCollection; } }
}