SORU
14 EKİM 2010, PERŞEMBE


Nasıl bir ConfigurationElementCollection ile bir ConfigurationSection uygulamak

Özel bir proje yapılandırma bölümü uygulamak için çalışıyorum ve hiçbir şey anlamıyorum, bu özel durumlara karşı koşmaya devam ediyorum. Birileri boşlukları doldurun umuyorum.

Uygulamam var.yapılandırma bu gibi görünüyor:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="ServicesSection" type="RT.Core.Config.ServicesConfigurationSectionHandler, RT.Core"/>
    </configSections>
    <ServicesSection type="RT.Core.Config.ServicesSection, RT.Core">
            <Services>
                <AddService Port="6996" ReportType="File" />
                <AddService Port="7001" ReportType="Other" />
            </Services>
        </ServicesSection>
</configuration>

ServiceConfig bir unsur gibi tanımlanmış var:

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

    public ServiceConfig(int port, string reportType)
    {
      Port = port;
      ReportType = reportType;
    }

    [ConfigurationProperty("Port", DefaultValue = 0, IsRequired = true, IsKey = true)]
    public int Port 
    {
      get { return (int) this["Port"]; }
      set { this["Port"] = value; }
    }

    [ConfigurationProperty("ReportType", DefaultValue = "File", IsRequired = true, IsKey = false)]
    public string ReportType
    {
      get { return (string) this["ReportType"]; }
      set { this["ReportType"] = value; }
    }
  }

Ve bir ServiceCollection böyle tanımlıyorum:

public class ServiceCollection : ConfigurationElementCollection
  {
    public ServiceCollection()
    {
      Console.WriteLine("ServiceCollection Constructor");
    }

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

    public void Add(ServiceConfig serviceConfig)
    {
      BaseAdd(serviceConfig);
    }

    public void Clear()
    {
      BaseClear();
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
      return ((ServiceConfig) element).Port;
    }

    public void Remove(ServiceConfig serviceConfig)
    {
      BaseRemove(serviceConfig.Port);
    }

    public void RemoveAt(int index)
    {
      BaseRemoveAt(index);
    }

    public void Remove(string name)
    {
      BaseRemove(name);
    }
  }

Kaçırdığım bölümü işleyicisi için harika bir yoldur. Aslında, IConfigurationSectionHandler bir uygulamaya çalıştım ama iki şey buldu

  1. işe yaramadı
  2. kaldırılmış.

Config verileri okuyabiliyorum yani tamamen ne yapacağına karar kayboldum. Herhangi bir yardım lütfen!

CEVAP
14 EKİM 2010, PERŞEMBE


Önceki cevap doğru ama kod olarak vereceğim.

App.config bu gibi görünmelidir:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections>
      <section name="ServicesSection" type="RT.Core.Config.ServiceConfigurationSection, RT.Core"/>
   </configSections>
   <ServicesSection>
      <Services>
         <add Port="6996" ReportType="File" />
         <add Port="7001" ReportType="Other" />
      </Services>
   </ServicesSection>
</configuration>

ServiceConfig ServiceCollection derslerin değişmeden kalır.

Yeni bir sınıf gerekir:

public class ServiceConfigurationSection : ConfigurationSection
{
   [ConfigurationProperty("Services", IsDefaultCollection = false)]
   [ConfigurationCollection(typeof(ServiceCollection),
       AddItemName = "add",
       ClearItemsName = "clear",
       RemoveItemName = "remove")]
   public ServiceCollection Services
   {
      get
      {
         return (ServiceCollection)base["Services"];
      }
   }
}

Ve bu hile yapmak gerekir. Kullanabilirsiniz onu tüketmek için:

ServiceConfigurationSection serviceConfigSection =
   ConfigurationManager.GetSection("ServicesSection") as ServiceConfigurationSection;

ServiceConfig serviceConfig = serviceConfigSection.Services[0];

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Canal de arkyoru

    Canal de ark

    18 Ocak 2007
  • parlophone

    parlophone

    28 ŞUBAT 2006
  • Slave Boy Films - Fandom from a Galaxy Far Far Away

    Slave Boy Fi

    12 HAZİRAN 2009