简体   繁体   English

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

[英]Weird error while accessing an element in a ConfigurationElementCollection

I have a ConfigurationSection with a ConfigurationElementCollection like below: 我有一个带有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>

and the code looks like this: 代码看起来像这样:

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

But when I try to access the "description" metadata, I get a NullReferenceException. 但是,当我尝试访问“描述”元数据时,会收到NullReferenceException。 This is because config.SampleConfig.Metadata["description"] is always null. 这是因为config.SampleConfig.Metadata [“ description”]始终为null。 But in the debug window, I can see that "Metadata" has 4 elements and "description" is there. 但是在调试窗口中,我可以看到“元数据”有4个元素,并且有“描述”。

This is also true while accessing "title". 访问“标题”时也是如此。 But "folderName" and "fileName" are fine!!! 但是“ folderName”和“ fileName”都可以!!!

I am not sure whats wrong and I am banging my head at this for the last 4 hours! 我不确定发生了什么问题,并且在过去4个小时里我一直为此感到震惊!

Please help me out here. 请帮我在这里。

Thanks. 谢谢。

Changing GetElementKey to: 将GetElementKey更改为:

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

Allows the following to return: TestTitle 允许以下内容返回:TestTitle

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

Figured it out!! 弄清楚了!!

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

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

Forgot to change the "SharePointName" when I renamed it to "Name" in the above code. 当我在上面的代码中将“ SharePointName”重命名为“ Name”时,忘记更改它。

@MethodMan and @Mike, thanks for the help! @MethodMan和@Mike,谢谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM