2012-01-01 7 views
1

マイページ要素のプロプライエタコレクション属性 "name"にアクセスする際に問題が発生しました。ページには属性を持つページフィールドのコレクションがあります。誰かが自分のコードを見て、それぞれに名前属性を持つページの集合を持ち、その値にアクセスする方法を教えてください。現時点では私のコードは何も返しませんが、ページは何のエラーもなくロードされるので、何が起こっているのか、属性フィールドを取得するのか分かりません。コレクション属性を返すカスタムWebConfigセクション

<configSections> 
    <sectionGroup name="site" type="MyProject.Configuration.Site"> 
    <section name="pages" type="MyProject.Configuration.Pages"/>  
    </sectionGroup> 
</configSections> 


<site> 
    <pages> 
    <page name="test">   
    </page> 
    </pages>  
</site> 

クラス:私の属性にアクセスするための

public class Site : ConfigurationSection 
{ 
    [ConfigurationProperty("pages")] 
    [ConfigurationCollection(typeof(PageCollection), AddItemName="page")] 
    public PageCollection Pages 
    { 
     get 
     { 
      return base["pages"] as PageCollection; 
     } 
    } 
} 

public class PageCollection : ConfigurationElementCollection 
{ 

    public PageCollection() 
    { 
     PageElement element = (PageElement)CreateNewElement(); 
     BaseAdd(element); // doesn't work here does if i remove it 
    } 

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

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

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

} 

public class PageElement : ConfigurationElement 
{ 
    public PageElement() { } 

    [ConfigurationProperty("name", IsKey=true, IsRequired=true)] 
    public string Name 
    { 
     get 
     { 
      return (string)base["name"]; 
     } 
     set 
     { 
      base["name"] = value; 
     } 
    } 
} 

コード:

Pages pageSettings = ConfigurationManager.GetSection("site/pages") as Pages; 
lblPage.Text = pageSettings.Page[0].Name; 

答えて

1

問題があなたのセクションの要素は、サイト、いないページでなければならないことである。

public class Site: ConfigurationSection 
{ 
    [ConfigurationProperty("pages")] 
    public PageCollection Page 
    { 
     get 
     { 
      return base["pages"] as PageCollection; 
     } 
    } 
} 

更新

それは対処するために必要ないくつかの問題があったことが判明:web.configファイルは、セクションの代わりに、sectiongroupとページ要素に変更する必要があります)

1を削除する必要があります。

<configSections> 
    <section name="site" type="MyProject.Configuration.Site, MyProject.Configuration"> 
    </section> 
    </configSections> 

2)サイトのクラスを変更する必要があり:

public class Site : ConfigurationSection 
{ 
    [ConfigurationProperty("pages")] 
    [ConfigurationCollection(typeof(PageCollection), AddItemName="page")] 
    public PageCollection pages 
    { 
     get 
     { 
      return base["pages"] as PageCollection; 
     } 
    } 
} 

3)PageCollectionコンストラクタは、そのコードが削除されている必要があります。

public PageCollection() 
    { 
    } 

4)nameプロパティはPageCollectionから削除するか、少なくとも必須ではないとマークする必要があります。

5)設定を取得するための呼び出しは以下のようになります。私はこれをテストし、これらの変更が正常に設定して読まれることを確認した

Site site = ConfigurationManager.GetSection("site") as Site; 

+0

コレクションの属性値がまだありません – ONYX

+0

私は自分のコードを更新しました。クラスディレクティブのPageCollectionクラス以外では動作します。 BaseAdd(要素)は機能しませんが、それを削除すると正常に動作するようにコードを修正する方法が分かります – ONYX

+0

@KDM:クラスディレクティブ(別名コンストラクタ)にコードを入れないでください。設定処理では自動的に適切なPageElementsが追加されるため、必要はありません。 –

関連する問題