在自定义配置部分中添加新集合

时间:2014-09-10 16:16:34

标签: c# configuration

我有一个自定义配置部分,我需要向它添加一个新的NameValue事件

这是层次结构

 -organizationConfigurations
 |-organizationConfigSelector
 |-specificConfigurations
  |-organizationConfig

现在我需要将一个名为 skipLanguages 的元素添加到 organizationConfig ,这只是一个简单的集合元素,它只是保存字符串。这是XML样本

<?xml version="1.0" encoding="utf-8" ?>

<matcher type="Host" match="HORNET" serviceName="config-hornet"/>
<matcher type="Parameter" match="VTDEV" serviceName="config-test-server"/>

<organizationConfig name="config-hornet">
  <add name="Organization_Identifier" value="SampleOrg"/>

  <add name="Allow_SelfRegistration" value="true"/>

  <add name="ApplicationSelector" value=""/>
  <add name="RegistrationSelector" value=""/>

  <add name="EnrollPinValidity" value="00.00:01:00"/>
  <add name="EnrollIncoming" value="0123456789"/>
  <add name="EnrollOutgoing" value="PIN"/>

  <add name="EnrollmentLockout" value="00.00:01:00"/>
  <add name="PinLockout" value="00.00:00:30"/>

  <skipLanguages>
    <add name="en"/>
    <add name="ur-UR" />
  </skipLanguages>

  <add name="Menu_Hide_Details" value="false"/>
  <add name="International_Format_Example" value="00971559559123"/>

  <add name="Service_Operator_Name" value="admin"/>
  <add name="Service_Operator_Password" value="admin"/>

  <add name="Service_Operator_Address_VTadp" value="https://hornet.org.com:1337/main/basicHttp"/>
  <add name="Service_Operator_Address_Admin" value="https://hornet.org.com:1337/Admn/basicHttp"/>
  <add name="Service_Operator_Address_PWR" value="https://hornet.org.com:1337/PWR/basicHttp"/>

</organizationConfig>

specificConfigurations &amp;的类 organizationConfig &amp;是:

    [ConfigurationCollection(typeof(OrganizationConfig), AddItemName = "organizationConfig")]
    public class SpecificConfigurations : ConfigurationElementCollection
    {
        #region Indexer
        public OrganizationConfig this[int index]
        {
            get { return BaseGet(index) as OrganizationConfig; }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }

                BaseAdd(index, value);
            }
        }
        #endregion

        #region Public Method(s)
        public OrganizationConfig GetServiceInfo(string serviceName)
        {
            return BaseGet(serviceName) as OrganizationConfig;
        }
        #endregion

        #region Overridden Method(s)
        protected override ConfigurationElement CreateNewElement()
        {
            return new OrganizationConfig();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((OrganizationConfig)element).Name;
        }
        #endregion
    }

    public class OrganizationConfig : ConfigurationSection
    {
        #region Configuration Properties
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return this["name"] as string; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("", IsDefaultCollection = true)]
        public NameValueConfigurationCollection Settings
        {
            get { return (NameValueConfigurationCollection)base[""]; }
        }
        #endregion
    }

1 个答案:

答案 0 :(得分:0)

目前,我将SkipLanguages添加为NameValue对,其值为分号分隔的字符串,其中包含要隐藏/阻止的语言。显然,这非常有效,并且也被铅所接受。所以,我用自己的答案完成了这个问题。