7

私は(うまくいけば)電子メールをキーとして自分のデザインのConfigurationElementCollectionをセットアップしました。それで?Web上で実際に見つけるのは難しい。どうすればよいですか:

  1. それを繰り返しますか?

  2. 特定の要素が存在するかどうかを確認しますか?

  3. 特定の要素を取得しますか?

...与えられた:

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

部分的な答え

1.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;
 }

小さな臭い。

4

2 に答える 2

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

もう少し作業すれば、辞書コレクションも作成できます。

于 2010-07-02T08:31:43.397 に答える
2

私はあなたの問題が何であるかを完全には理解していません-しかし、基本的に、カスタム構成要素がある場合は、次のようなものを使用して構成ファイルからそれを取得できるはずです:

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

構成要素を取得したら、それを好きなように使用できます。要求したすべてのことを実装できます。要素の存在を確認したり、特定の要素を取得したりできます。

詳細については、CodeProjectでの.NET2.0構成に関するJonRistaの3部構成のシリーズも確認する必要があります。これらの記事は構成の「課題」のロックを解除するのに役立つかもしれません;-)

強くお勧めします、よく書かれていて、非常に役に立ちます!

そして、まだ発見していない場合は、Codeplexに優れたConfiguration Section Designerがあり、構成セクションとコレクションを視覚的に簡単に設計し、すべてのねばねばしたグルーコードを作成できます。これは非常に便利です。

マーク

于 2010-05-06T20:26:02.670 に答える