C# 自定义配置部分的问题

C# 自定义配置部分的问题,c#,app-config,C#,App Config,我试图创建一个相当简单的自定义配置部分。我的班级是: namespace NetCenterUserImport { public class ExcludedUserList : ConfigurationSection { [ConfigurationProperty("name")] public string Name { get { return (string)base["name"]; }

我试图创建一个相当简单的自定义配置部分。我的班级是:

namespace NetCenterUserImport
{
    public class ExcludedUserList : ConfigurationSection
    {
        [ConfigurationProperty("name")]
        public string Name
        {
            get { return (string)base["name"]; }
        }

        [ConfigurationProperty("excludedUser")]
        public ExcludedUser ExcludedUser
        {
            get { return (ExcludedUser)base["excludedUser"]; }
        }

        [ConfigurationProperty("excludedUsers")]
        public ExcludedUserCollection ExcludedUsers
        {
            get { return (ExcludedUserCollection)base["excludedUsers"]; }
        }
   }

   [ConfigurationCollection(typeof(ExcludedUser), AddItemName = "add")]
   public class ExcludedUserCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ExcludedUserCollection();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ExcludedUser)element).UserName;
        }
    }

    public class ExcludedUser : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public string UserName
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }
    }
}
我的app.config是:

  <excludedUserList name="test">
<excludedUser name="Hello" />
<excludedUsers>
  <add name="myUser" />
</excludedUsers>
我听到一个例外的说法

“无法识别的属性‘名称’。”


我确信我遗漏了一些简单的东西,但我已经在这里查看了十几个示例和答案,似乎找不到我的错误所在。

在ExcludedUserCollection.CreateNewElement方法中,您正在创建ExcludedUserCollection实例,它应该是单个元素,例如:

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

如上所述更改方法对我很有效。

本节的开头是
excludedUserList
吗?如果是这样的话,我不认为你可以有一个属性附加到它。好吧,不是真的-检查这里:即使没有在excludedUserList中的属性,我也会得到同样的错误。我只是想确定错误出现的确切时间。我在节中添加了一个自定义属性,然后在节中添加了自定义属性,当这两个属性都起作用时,我又添加了自定义集合,所有内容都再次中断。太棒了!非常感谢。我知道我忽略了一些简单的事情,但我办公室里唯一的另一个开发人员中风了,所以我没有人来检查像这个ATM机这样的东西。
protected override ConfigurationElement CreateNewElement()
{
    return new ExcludedUser();
}