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

时间:2015-04-24 18:19:26

标签: c# asp.net configurationsection custom-configuration configurationelement

我有一个带有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; }
        }
}

但是当我尝试访问&#34;描述&#34;元数据,我得到一个NullReferenceException。这是因为config.SampleConfig.Metadata [&#34; description&#34;]始终为null。但是在调试窗口中,我可以看到&#34;元数据&#34;有4个元素和&#34;描述&#34;在那里。

访问&#34; title&#34;时也是如此。但&#34; folderName&#34;和&#34; fileName&#34;很好!!!

我不确定什么是错的,我在过去的4个小时里一直在敲我的脑袋!

请帮帮我。

感谢。

2 个答案:

答案 0 :(得分:0)

将GetElementKey更改为:

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

允许以下内容返回:TestTitle

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

答案 1 :(得分:0)

想出来!!

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

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

忘记更改&#34; SharePointName&#34;当我把它重命名为&#34; Name&#34;在上面的代码中。

@MethodMan和@Mike,谢谢你的帮助!