从ConfigurationElementCollection获取配置元素

Mar*_*tin 7 c# system.configuration configurationelement

我(希望)设置我自己的设计的ConfigurationElementCollection,电子邮件作为键.怎么办?很难在网上找到.我如何能:

  1. 通过它迭代?

  2. 查看是否存在特定元素?

  3. 得到一个特定的元素?

...给出:

    YourConfigElement config = 
   ConfigurationManager.GetSection("YourSectionName") as YourConfigElement;
Run Code Online (Sandbox Code Playgroud)

部分答案

1.

 foreach (X x in config.XCollection) 
             <code here>
Run Code Online (Sandbox Code Playgroud)

2.用"替换此处的代码"代替

 { 
    if (x.Y == needle) 
    {
       hasIndeed = true;
       break;
    }
 }
Run Code Online (Sandbox Code Playgroud)

3.用"替换此处的代码"代替

 { if (x.Y == needle) 
       cameUpWith = x;
       break;
 }
Run Code Online (Sandbox Code Playgroud)

微小的气味.

Bro*_*ski 8

你想要的是你自己的通用ConfigurationElementCollection基类,它实现了IList<T>.然后,您可以从此继承所有配置集合,并减少创建配置集合时需要执行的工作量.

public abstract class BaseConfigurationElementCollection<TConfigurationElementType> : ConfigurationElementCollection, IList<TConfigurationElementType> where TConfigurationElementType : ConfigurationElement, IConfigurationElementCollectionElement, new()
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new TConfigurationElementType();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((TConfigurationElementType)element).ElementKey;
    }

    public IEnumerator<TConfigurationElement> GetEnumerator()
    {
        foreach (TConfigurationElement type in this)
        {
            yield return type;
        }
    }

    public void Add(TConfigurationElementType configurationElement)
    {
        BaseAdd(configurationElement, true);
    }

    public void Clear()
    {
        BaseClear();
    }

    public bool Contains(TConfigurationElementType configurationElement)
    {
        return !(IndexOf(configurationElement) < 0);
    }

    public void CopyTo(TConfigurationElementType[] array, int arrayIndex)
    {
        base.CopyTo(array, arrayIndex);
    }

    public bool Remove(TConfigurationElementType configurationElement)
    {
        BaseRemove(GetElementKey(configurationElement));

        return true;
    }

    bool ICollection<TConfigurationElementType>.IsReadOnly
    {
        get { return IsReadOnly(); }
    }

    public int IndexOf(TConfigurationElementType configurationElement)
    {
        return BaseIndexOf(configurationElement);
    }

    public void Insert(int index, TConfigurationElementType configurationElement)
    {
        BaseAdd(index, configurationElement);
    }

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

    public TConfigurationElementType this[int index]
    {
        get
        {
            return (TConfigurationElementType)BaseGet(index);
        }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

通过更多的工作,您也可以拥有字典集合.


mar*_*c_s 2

我不完全理解您的问题是什么 - 但基本上,如果您有自定义配置元素,您应该能够使用以下内容从配置文件中检索它:

YourConfigElement config = 
    ConfigurationManager.GetSection("YourSectionName") as YourConfigElement ;
Run Code Online (Sandbox Code Playgroud)

一旦你有了配置元素,你就可以用它做任何你喜欢的事情 - 你可以实现你要求的所有事情 - 检查元素是否存在,获取特定元素等。

您还应该查看 CodeProject 上 Jon Rista 关于 .NET 2.0 配置的三部分系列,以获取更多信息 - 也许这些文章将帮助您解决配置“挑战”;-)

强烈推荐,写得很好,非常有帮助!

如果您还没有发现它 - Codeplex 上有一个出色的配置节设计器,它使可视化设计配置节和集合变得轻而易举,并为您编写所有粘稠的粘合代码 - 确实非常方便!

马克