簡體   English   中英

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

但是,當我嘗試訪問“描述”元數據時,會收到NullReferenceException。 這是因為config.SampleConfig.Metadata [“ description”]始終為null。 但是在調試窗口中,我可以看到“元數據”有4個元素,並且有“描述”。

訪問“標題”時也是如此。 但是“ folderName”和“ fileName”都可以!!!

我不確定發生了什么問題,並且在過去4個小時里我一直為此感到震驚!

請幫我在這里。

謝謝。

將GetElementKey更改為:

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

允許以下內容返回: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);
    }

當我在上面的代碼中將“ SharePointName”重命名為“ Name”時,忘記更改它。

@MethodMan和@Mike,謝謝您的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM