7

子孫CustomSetting要素の親ConfigurationSectionで属性セットを取得して使用するにはどうすればよいですか? CustomSetting要素がValueプロパティを返すときに、この属性が必要です。子孫要素に.NETカスタムConfigurationElementプロパティを使用するにはどうすればよいですか?

私はこのようにApp.configファイルをフォーマットします:

<CustomSettings someProperty="foo"> 
    <CustomSetting key="bar" value="fermeneba" /> 
    <CustomSetting key="laa" value="jubaduba" /> 
</CustomSettings> 

私はCustomSettingクラスからsomePropertyの属性にアクセスする方法を見つけることができないことを除いて、コードの作業を持っています。私が見つけた唯一の方法は、これまでのところ、乱雑である、このような構成をフォーマットすることです:

<CustomSettings> 
    <CustomSetting someProperty="foo" key="bar" value="fermeneba" /> 
    <CustomSetting someProperty="foo" key="laa" value="jubaduba" /> 
</CustomSettings> 

答えて

12

System.Configuration APIが許可されていませんので、これはそれがあるべきよりも困難で達成ConfigurationElementから親にナビゲートします。したがって、親要素でその情報にアクセスする場合は、その関係を手動で作成する必要があります。

public class CustomSettingsSection : ConfigurationSection 
{ 
    [ConfigurationProperty("someProperty", DefaultValue="")] 
    public string SomeProperty 
    { 
     get { return (string)base["someProperty"]; } 
     set { base["someProperty"] = value; } 
    } 

    [ConfigurationProperty("", IsDefaultCollection = true)] 
    public CustomSettingElementCollection Elements 
    { 
     get 
     { 
      var elements = base[""] as CustomSettingElementCollection; 
      if (elements != null && elements.Section == null) 
       elements.Section = this; 
      return elements; 
     } 
    } 
} 

public class CustomSettingElementCollection : ConfigurationElementCollection 
{ 

    internal CustomSettingsSection Section { get; set; } 

    public override ConfigurationElementCollectionType CollectionType 
    { 
     get { return ConfigurationElementCollectionType.BasicMap; } 
    } 

    public CustomSettingElement this[string key] 
    { 
     get { return BaseGet(key) as CustomSettingElement; } 
    } 

    protected override ConfigurationElement CreateNewElement() 
    { 
     return new CustomSettingElement { Parent = this }; 
    } 

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

    protected override string ElementName 
    { 
     get { return "customSetting"; } 
    } 
} 

public class CustomSettingElement : ConfigurationElement 
{ 

    internal CustomSettingElementCollection Parent { get; set; } 

    public string SomeProperty 
    { 
     get 
     { 
      if (Parent != null && Parent.Section != null) 
       return Parent.Section.SomeProperty; 
      return default(string); 
     } 
    } 




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

    [ConfigurationProperty("value", DefaultValue = "")] 
    public string Value 
    { 
     get { return (string)base["value"]; } 
     set { base["value"] = value; } 
    } 

} 

あなたはCustomSettingElementCollectionセクションのElementsゲッターに設定されますSection性質を持っていることがわかります。私は一緒にあなたの質問のconfigスニペットのためにそれを行うサンプル実装を入れています。 CustomSettingElementは、コレクションのCreateNewElement()メソッドで設定されるParentプロパティを持っています。

これにより、要素の実際のConfigurationPropertyに対応していないにもかかわらず、関係ツリーを見て、要素にSomePropertyプロパティを追加することができます。

あなたの問題を解決する方法がわかります

+0

これは完全に機能しました。ありがとうございました! –

関連する問題