12

いくつかのクラスに基づくカスタム構成があります。私の問題は、構成要素が認識されていないというエラーが表示されることです。クラスは次のとおりです。

[ConfigurationCollection(typeof(SectionItem), AddItemName = "Item", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class Sections : ConfigurationElementCollection
{
    public SectionItem this[int index]
    {
        get { return BaseGet(index) as SectionItem; }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }

    public new SectionItem this[string response]
    {
        get { return (SectionItem)BaseGet(response); }
        set
        {
            if (BaseGet(response) != null)
            {
                BaseRemoveAt(BaseIndexOf(BaseGet(response)));
            }
            BaseAdd(value);
        }
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((SectionItem)element).Key;
    }
  }

そしてSectionItemクラス:

public class SectionItem : ConfigurationElement
{
    [ConfigurationProperty("key", IsRequired = true, IsKey = true)]
    public string Key
    {
        get { return this["key"] as string; }
    }
}

SectionItemsクラスにタイプの構成プロパティを追加すると、構成ファイルのタグ内にSections複数のプロパティが必要になるため、機能しません。解決策を探しましたが、見つけたものはすべてこれでうまくいきませんでした。私が達成しようとしていることをよりよく理解するために、これは私の構成がどのように見えるかです:SectonItemsSection

<configuration>
 <configSections>
  <section name="AdminConfig" type="XmlTest.AdminConfig, XmlTest"/>
 </configSections>
 <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
 </startup>

<AdminConfig>
 <Field name="field1" key="12345" path="asd"/>
  <Section>
    <Item key="12345"/>
    <Item key="54321"/>
  </Section>
 </AdminConfig>  
</configuration>
4

1 に答える 1

16

問題が見つかりました。Sectionsクラスにはこれがありましたが、次のよう[ConfigurationCollection(typeof(SectionItem), AddItemName = "Item", CollectionType = ConfigurationElementCollectionType.BasicMap)]に、クラスのプロパティに注釈を付ける必要がありました。ConfigurationSection

 [ConfigurationProperty("Section")]
 [ConfigurationCollection(typeof(Sections), AddItemName = "Item")]
 public Sections Sections
 {
    get
    {
      return (Sections)this["Section"];
    }
 }

アイテムが認識され、適切に機能するようになりました。

于 2013-10-01T11:25:12.303 に答える