C#自定义配置:“已添加条目”。

时间:2017-05-11 12:26:04

标签: c# app-config configurationmanager

我做了一个小的自定义配置设置,我不断收到错误“已添加条目”。当我尝试使用我的自定义集合时。我的代码看起来像这样。

问题来自我的标签。

我没有看到我所缺少的东西,因为我实现了同样的东西,而且这个完美无缺。

如果有帮助,我的.NET版本是4.0。

有问题的应用配置部分:

<WorkersCollectionSection>
<WorkersList>
  <Worker name="Category" isEnabled="false"
        assemblyNamespace="xxxxxx.xxxxxxxxxxxxxxx.Models.Category"
        queueName="CategorQueue"
        saveToFolder="false">
    <HandlesList>
      <Handle name="xxxxxxx" isEnabled="true"/>
      <Handle name="yyyyyyy" isEnabled="true"/>
      <Handle name="zzzzzzzzzz" isEnabled="true"/>
    </HandlesList>
  </Worker>
 <WorkersList>
<WorkersCollectionSection>

财产的定义:

    [ConfigurationProperty("HandlesList")]
    [ConfigurationCollection(typeof(WorkerCollection), AddItemName = "Handle")]
    public HandleCollection HandleCollection
    {
        get { return (HandleCollection) base["HandlesList"]; }
    }

标记的代码:`public class HandleCollection:ConfigurationElementCollection,IEnumerable     {

    public HandleCollection()
    {
        HandleElement handle = (HandleElement)CreateNewElement();
        BaseAdd(handle);
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
    }

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

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

    public HandleElement this[int index]
    {
        get { return (HandleElement)BaseGet(index); }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }

    public new HandleElement this[string Name]
    {
        get { return (HandleElement)BaseGet(Name); }
    }

    public int IndexOf(HandleElement handle)
    {
        return BaseIndexOf(handle);
    }

    public void Add(HandleElement url)
    {
        BaseAdd(url);
    }

    protected override void BaseAdd(ConfigurationElement element)
    {
        BaseAdd(element, false);
    }

    public void Remove(HandleElement handle)
    {
        if (BaseIndexOf(handle) >= 0)
            BaseRemove(handle);
    }

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

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

    public void Clear()
    {
        BaseClear();
    }

    IEnumerator<ConfigurationElement> IEnumerable<ConfigurationElement>.GetEnumerator()
    {
        return (from i in Enumerable.Range(0, this.Count)
                select this[i])
            .GetEnumerator();
    }

    protected override string ElementName
    {
        get { return "Handle"; }
    }

    public static explicit operator HandleCollection(Dictionary<string, string> v)
    {
        throw new NotImplementedException();
    }

    public static explicit operator HandleCollection(ConfigurationSection v)
    {
        throw new NotImplementedException();
    }
}`

列表中句柄元素的代码:

 public class HandleElement: ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 0, MaxLength = 60)]
    public string Name
    {
        get { return base["name"] as string; }
        set { base["name"] = value; }
    }

    [ConfigurationProperty("isEnabled", IsRequired = true)]
    public bool IsEnabled
    {
        get { return (bool)base["isEnabled"]; }
        set { base["isEnabled"] = value; }
    }

}

1 个答案:

答案 0 :(得分:0)

似乎问题是我没有检查集合添加方法来检查我添加的元素是否具有与空元素不同的密钥。我将我的集合构造函数更改为:

 public HandleCollection()
    {
        HandleElement handle = (HandleElement)CreateNewElement();
        if (handle.Name != "")
            BaseAdd(handle);
    }

现在似乎工作正常。