SORU
27 NİSAN 2010, Salı


Özel uygulama." basit bir liste ile config bölümünde;Ekle" unsurları

Nasıl özel bir uygulama oluşturabilirim.config bölümünde sadece add elementlerin basit bir liste var mı?

Birkaç örnek (*13 örneğin*) bu gibi özel bölümler için bulduk:

<RegisterCompanies>
  <Companies>
    <Company name="Tata Motors" code="Tata"/>
    <Company name="Honda Motors" code="Honda"/>
  </Companies>
</RegisterCompanies>

Ama nasıl fazladan tahsilat elemanı düşmemek için ne yapmam gerekir ("Şirket") appSettings connectionStrings bölümler aynı görünüyor? Diğer bir deyişle, istiyorum:

<registerCompanies>
  <add name="Tata Motors" code="Tata"/>
  <add name="Honda Motors" code="Honda"/>
</registerCompanies>

CEVAP
15 ŞUBAT 2013, Cuma


OP config dosyasını temel alan kodu ile tam bir örnek:

<configuration>
    <configSections>
        <section name="registerCompanies" 
                 type="My.MyConfigSection, My.Assembly" />
    </configSections>
    <registerCompanies>
        <add name="Tata Motors" code="Tata"/>
        <add name="Honda Motors" code="Honda"/>
    </registerCompanies>
</configuration>

Burada çökmüş koleksiyonu ile özel yapılandırma bölümü uygulamak için örnek kod

using System.Configuration;
namespace My {
public class MyConfigSection : ConfigurationSection {
    [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
    public MyConfigInstanceCollection Instances {
        get { return (MyConfigInstanceCollection)this[""]; }
        set { this[""] = value; }
    }
}
public class MyConfigInstanceCollection : ConfigurationElementCollection {
    protected override ConfigurationElement CreateNewElement() {
        return new MyConfigInstanceElement();
    }

    protected override object GetElementKey(ConfigurationElement element) {
        //set to whatever Element Property you want to use for a key
        return ((MyConfigInstanceElement)element).Name;
    }
}

public class MyConfigInstanceElement : ConfigurationElement {
    //Make sure to set IsKey=true for property exposed as the GetElementKey above
    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name {
        get { return (string) base["name"]; }
        set { base["name"] = value; }
    }

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

Burada kod yapılandırma bilgilere erişmek için nasıl bir örnek.

var config = ConfigurationManager.GetSection("registerCompanies") 
                 as MyConfigSection;

Console.WriteLine(config["Tata Motors"].Code);
foreach (var e in config.Instances) { 
   Console.WriteLine("Name: {0}, Code: {1}", e.Name, e.Code); 
}

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Machinima

    Machinima

    17 Ocak 2006
  • Matt Stokes

    Matt Stokes

    22 Ocak 2008
  • TheMasterOfHell100

    TheMasterOfH

    13 AĞUSTOS 2011