Dot-Net

如何在後代元素上使用 .NET 自定義 ConfigurationElement 屬性?

  • August 13, 2011

如何在後代 CustomSetting 元素的父 ConfigurationSection 中獲取和使用屬性集?當 CustomSetting 元素返回 Value 屬性時,我需要此屬性。

我想像這樣格式化 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>

由於 System.Configuration API 不允許您從 a 導航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有一個Section在該部分的Elementsgetter 中設置的屬性。反過來CustomSettingElement,該Parent屬性具有在集合的CreateNewElement()方法中設置的屬性。

這樣就可以向上遍歷關係樹並向元素添加一個SomeProperty屬性,即使該屬性與該元素上的實際 ConfigurationProperty 不對應。

希望能給你一個想法如何解決你的問題!

引用自:https://stackoverflow.com/questions/7044871