带有自定义配置部分的配置文件中无法识别的元素“项目”

我有一个基于某些类的自定义配置。 我的问题是我得到一个错误,说配置元素无法识别。 课程如下:

[ConfigurationCollection(typeof(SectionItem), AddItemName = "Item", CollectionType = ConfigurationElementCollectionType.BasicMap)] public class Sections : ConfigurationElementCollection { public SectionItem this[int index] { get { return BaseGet(index) as SectionItem; } set { if (BaseGet(index) != null) { BaseRemoveAt(index); } BaseAdd(index, value); } } public new SectionItem this[string response] { get { return (SectionItem)BaseGet(response); } set { if (BaseGet(response) != null) { BaseRemoveAt(BaseIndexOf(BaseGet(response))); } BaseAdd(value); } } protected override ConfigurationElement CreateNewElement() { return new SectionItem(); } protected override object GetElementKey(ConfigurationElement element) { return ((SectionItem)element).Key; } } 

SectionItem类:

 public class SectionItem : ConfigurationElement { [ConfigurationProperty("key", IsRequired = true, IsKey = true)] public string Key { get { return this["key"] as string; } } } 

如果我在Sections类中添加一个类型为SectionItems的配置属性对我不起作用,因为我想在配置文件的Section标签内有多个SectonItems 。 我已经搜索了解决方案,但我发现的一切都没有用这个来解决问题。 为了更好地理解我想要实现的目标,我的配置看起来如何:

   

好的,所以我发现了问题。 虽然Sections类有[ConfigurationCollection(typeof(SectionItem), AddItemName = "Item", CollectionType = ConfigurationElementCollectionType.BasicMap)]我必须在ConfigurationSection类中注释该属性,如下所示:

  [ConfigurationProperty("Section")] [ConfigurationCollection(typeof(Sections), AddItemName = "Item")] public Sections Sections { get { return (Sections)this["Section"]; } } 

现在物品被识别,物品正常运作。