访问 ConfigurationElementCollection 中的元素时出现奇怪的错误

Weird error while accessing an element in a ConfigurationElementCollection

我有一个带有 ConfigurationElementCollection 的 ConfigurationSection,如下所示:

    <MyConfiguration hostUrl="https://example.com/ewe/rets">
        <sampleConfig userName="xzxzxzxzxzx" password="00000000" listName="MyList">
          <metadata>
            <add name="folderName" value="TestFolder"/>
            <add name="fileName" value="TestFileName" />
            <add name="title" value="TestTitle" />
            <add name="description" value="TestDescription" />
          </metadata>
        </sampleConfig>
        <whateverConfig userName="xzxzxzxzxzx" password="00000000" listName="MyList">
          <metadata>
            <add name="siteName" value="TestFolder"/>
            <add name="fileName" value="TestFileName" />
            <add name="title" value="TestTitle" />
            <add name="description" value="TestDescription" />
          </metadata>
        </whateverConfig>
</MyConfiguration>

代码如下所示:

    public sealed class MyConfiguration: ConfigurationSection
    {
            [ConfigurationProperty("hostUrl", IsRequired = true)]
            public string HostUrl
            {
                get { return (string)base["hostUrl"]; }
                set { base["hostUrl"] = value; }
            }

            [ConfigurationProperty("sampleConfig", IsRequired = true)]
            public ApiConfig SampleConfig
            {
                get { return (ApiConfig)base["sampleConfig"]; }
                set { base["sampleConfig"] = value; }
            }

            [ConfigurationProperty("whateverConfig", IsRequired = true)]
            public ApiConfig WhateverConfig
            {
                get { return (ApiConfig)base["whateverConfig"]; }
                set { base["whateverConfig"] = value; }
            }
    }

        public class ApiConfig : ConfigurationElement
        {
            [ConfigurationProperty("userName", IsRequired = true)]
            public string UserName
            {
                get { return (string)base["userName"]; }
                set { base["userName"] = value; }
            }

            [ConfigurationProperty("password", IsRequired = true)]
            public string Password
            {
                get { return (string)base["password"]; }
                set { base["password"] = value; }
            }

            [ConfigurationProperty("listName", IsRequired = true)]
            public string ListName
            {
                get { return (string)base["listName"]; }
                set { base["listName"] = value; }
            }

                    [ConfigurationProperty("metadata", IsDefaultCollection = false)]
            [ConfigurationCollection(typeof(MetadataCollection),
                AddItemName = "add",
                ClearItemsName = "clear",
                RemoveItemName = "remove")]
            public MetadataCollection MetaData
            {
                get
                {
                    return (MetadataCollection)base["metadata"];
                }
            }
    }

public class MetadataCollection : ConfigurationElementCollection
    {
        public MetadataConfig this[string key]
        {
            get { return (MetadataConfig)BaseGet(key); }
            set
            {
                if (BaseGet(key) != null)
                {
                    BaseRemove(key);
                }
                BaseAdd(value);
            }
        }

        public void Add(MetadataConfig metadataConfig)
        {
            BaseAdd(metadataConfig);
        }

        public void Clear()
        {
            BaseClear();
        }

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

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((MetadataConfig)element).SharePointName;
        }

        public void Remove(MetadataConfig element)
        {
            BaseRemove(element.SharePointName);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

    }

public class MetadataConfig : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return (string)base["name"]; }
            set { base["name"] = value; }
        }

        [ConfigurationProperty("value", IsRequired = true, IsKey = false)]
        public string Value
        {
            get { return (string)base["value"]; }
            set { base["value"] = value; }
        }
}

但是当我尝试访问 "description" 元数据时,我得到了 NullReferenceException。这是因为 config.SampleConfig.Metadata["description"] 始终为空。但是在调试 window 中,我可以看到 "Metadata" 有 4 个元素并且 "description" 在那里。

访问 "title" 时也是如此。但是 "folderName" 和 "fileName" 没问题!!!

我不确定出了什么问题,在过去的 4 个小时里我一直在为此苦苦思索!

请帮帮我。

谢谢。

将 GetElementKey 更改为:

protected override object GetElementKey(ConfigurationElement element)
{
    return ((MetadataConfig)element).Name;
}

允许以下内容 return:TestTitle

Console.WriteLine(serviceConfigSection.SampleConfig.MetaData["title"].Value);

想通了!!

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((MetadataConfig)element).Name;
    }

    public void Remove(MetadataConfig element)
    {
        BaseRemove(element.Name);
    }

我在上面的代码中重命名为"Name"时忘记更改"SharePointName"。

@MethodMan 和@Mike,感谢您的帮助!