代码之家  ›  专栏  ›  技术社区  ›  theDoron1

C#应用程序。配置-未获取课程集合的值

c#
  •  2
  • theDoron1  · 技术社区  · 6 年前

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

    我已经按照我在网站上看到的方式设置了配置和类。我可以获取学校名称、地址、城市和州的值,但无法获取设置。课程值,并在按原样运行代码时出现以下错误:

    未处理的异常

    '系统。学校博客中出现TypeInitializationException“”。exe。其他信息:“Schoolbog”的类型初始值设定项。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 类继承自 ConfigurationElement 这个 CouseElement 类与我们之前创建的其他元素类非常相似。尽管我不会详细介绍 课程元素 类,请注意 IsRequired 每个的属性 ConfigurationProperty 属性一个设置为true,另一个设置为false。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Stringfellow    6 年前

    的类 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; } }
    }