4

을 목록에 나는 Web.config의이 있습니다읽기 Web.config의 섹션

<MySection> 
    <Setting1 Value="10" /> 
    <Setting2 Value="20" /> 
    <Setting3 Value="30" /> 
    <Setting4 Value="40" /> 
</MySection> 

나는 List<string> (예 : 모든 값을 모두 "절에서 MySection"를 읽고 얻을 싶습니다 : "10" "20", "30")

감사합니다, 모두의

+1

Nothing ... Google에서 검색했지만 답변을 찾지 못했습니다 (또는 올바른 키워드를 사용하지 않았습니다). –

+1

Google [ "custom con (http://www.google.com/search?q=custom+configuration+section) – abatishchev

답변

5

첫째, 나는 Unity Configuration를 사용하여 사용을 권장합니다.

코드 :

public class MySection : ConfigurationSection 
{ 
    protected static ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection(); 

    private static ConfigurationProperty propElements = new ConfigurationProperty("elements", typeof(MyElementCollection), null, ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsDefaultCollection); 

    static BotSection() 
    { 
     properties.Add(propElements); 
    } 

    [ConfigurationProperty("elements", DefaultValue = null, IsRequired = true)] 
    [ConfigurationCollection(typeof(MyElementCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")] 
    public MyElementCollection Elements 
    { 
     get 
     { 
      return (MyElementCollection)this[propElements]; 
     } 
     set 
     { 
      this[propElements] = value; 
     } 
    } 
} 

public class MyElementCollection : ConfigurationElementCollection, 
            IEnumerable<ConfigurationElement> // most important difference with default solution 
{ 
    public void Add(MyElement element) 
    { 
     base.BaseAdd(element); 
    } 

    public void Clear() 
    { 
     base.BaseClear(); 
    } 

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

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((MyElement)element).Id; 
    } 

    IEnumerator<MyElement> IEnumerable<MyElement>.GetEnumerator() 
    { 
     return this.OfType<MyElement>().GetEnumerator(); 
    } 
} 

public class MyElement : ConfigurationElement 
{ 
    protected static ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection(); 

    private static ConfigurationProperty propValue= new ConfigurationProperty("value", typeof(int), -1, ConfigurationPropertyOptions.IsRequired); 

    public int Value 
    { 
     get 
     { 
      return (int)this[propValue]; 
     } 
     set 
     { 
      this[propValue] = value; 
     } 
    } 
} 

구성 :

<configuration> 
    <configSections> 
     <section name="MySection" type="MySection, MyAssembly"/> 
    </configSections> 
    <MySection> 
     <elements> 
      <clear /> 
      <add value="10" /> 
      <remove value="10" /> 
      <add value="20" /> 
      <add value="30" /> 
     </elements> 
    </MySection> 
</configuration> 
+0

아니면 작동하지 않거나 조각이 없거나 바보입니다. 작동하지 않습니다. –

+0

@ Kris-I : 더 많은 코드를 추가했지만 주제에 관해 더 많이 읽고, 더 많은 것을 직접 해보고, 어떤 결과가 있거나 오류가 있다면 – abatishchev

+0

@ Kris-I : 여기를보세요. MyElementCollection에 이미 Elements가 포함되어 있으며 이제는 MyAttribute 속성을 추가합니다. – abatishchev

2

난 당신이 코드 플렉스에 우수한 오픈 소스 Configuration Section Designer 프로젝트를 살펴 권하고 싶습니다. Visual Studio에서 호스팅되는 디자이너를 사용하여 사용자 지정 구성 섹션을 만들 수 있습니다.

예를 들어, 다음과 같은 사용자 지정 구성 섹션 디자인 :

Simple Custom Section 은 다음과 같이 구성 파일에 발생합니다 :

foreach (MySetting setting in MySection.Instance.Items) 
{ 
    Console.WriteLine("{0}: {1}", setting.Name, setting.Value); 
} 
: 프로그램이 같은 소비 할 수있는

<?xml version="1.0"?> 
<configuration> 
    <configSections> 
    <section name="MySection" type="MyNamespace.MySection, MyAssembly"/> 
    </configSections> 
    <MySection xmlns="urn:MyNamespace"> 
    <MySetting Name="Test1" Value="One" /> 
    <MySetting Name="Test2" Value="Two" /> 
    </MySection> 
</configuration> 

+0

MySection은 현재 컨텍스트에 존재하지 않습니다 ... –

+0

@ Kris-I : 물론 존재하지 않습니다. 먼저 정의해야합니다. – abatishchev