从 ConfigurationElementCollection 获取配置元素

发布于 2024-08-31 13:24:29 字数 721 浏览 2 评论 0原文

我(希望)已经设置了我自己设计的 ConfigurationElementCollection,并以电子邮件作为键。现在怎么办?其实网上很难找到。我如何:

  1. 迭代它?

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

  3. 获取特定元素?

...给出:

    YourConfigElement config = 
   ConfigurationManager.GetSection("YourSectionName") as YourConfigElement;

部分答案

1.

 foreach (X x in config.XCollection) 
             <code here>

2 .将“此处的代码”替换为

 { 
    if (x.Y == needle) 
    {
       hasIndeed = true;
       break;
    }
 }

3 。将“此处的代码”替换为“

 { if (x.Y == needle) 
       cameUpWith = x;
       break;
 }

微小的气味”。

I have (hopefully) setup ConfigurationElementCollection of my own design with emails as keys. Now what? Hard to find actually on the web. How do I:

  1. iterate through it?

  2. See if a specific element exists?

  3. get a specific element?

...given:

    YourConfigElement config = 
   ConfigurationManager.GetSection("YourSectionName") as YourConfigElement;

Partial answer

1.

 foreach (X x in config.XCollection) 
             <code here>

2 . replace "code here" with

 { 
    if (x.Y == needle) 
    {
       hasIndeed = true;
       break;
    }
 }

3 . replace "code here" with

 { if (x.Y == needle) 
       cameUpWith = x;
       break;
 }

Tiny odor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝梦月影 2024-09-07 13:24:29

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

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

再多做一点工作,您也可以拥有一个字典集。

What you want is your own generic ConfigurationElementCollection base class which implements IList<T>. You can then inherit from this for all your configuration collections and cut down on the amount of work you need to do when creating configuration collections.

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

With a little bit more work you can have a dictionary collection as well.

您的好友蓝忘机已上羡 2024-09-07 13:24:29

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

YourConfigElement config = 
    ConfigurationManager.GetSection("YourSectionName") as YourConfigElement ;

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

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

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

如果您还没有发现它 - Codeplex 上有一个出色的配置部分设计器,它可以直观地设计配置快速编辑部分和集合,并为您编写所有粘稠的粘合代码 - 确实非常方便!

马克

I don't totally understand what your issues are - but basically, if you have a custom configuration element, you should be able to retrieve that from the config file using something like:

YourConfigElement config = 
    ConfigurationManager.GetSection("YourSectionName") as YourConfigElement ;

Once you have your configuration element, you can do with it whatever you like - you can implement all those things you asked for - check existance of an element, get a specific element etc.

You should also check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject for more information - maybe those articles will help you unlock your config "challenge" ;-)

Highly recommended, well written and extremely helpful!

And if you haven't discovered it already - there's an excellent Configuration Section Designer up on Codeplex which makes visually designing configuration sections and collections a snap and writes all the gooey glue code for you - very handy indeed!

Marc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文