2010-11-25 1 views
0

Windows Forms 응용 프로그램의 app.config 파일에 사용자 지정 섹션을 추가했습니다.사용자 지정 구성 설정 문제

CustomFields myCustomFields = (CustomFields)System.Configuration.ConfigurationManager.GetSection("CustomFields"); 

내가 섹션 이름을 지정합니다 : 나는 문제가 생각하는 경우 지금 여기

<section name="CustomFields" type="Application.Core.CustomFields, ATMCardRequest.Core" allowLocation="true" allowDefinition="Everywhere" /> 

가 나는 구성 파일을 확장하는 클래스를 만들었습니다. 위의 전에 잘 작동하지만이 섹션 대신이 일의 특성이 많이 필요했습니다

<CustomFields> 
<property name="setting1">hello</property> 
<property name="setting2">world</property> 
... 
</CustomFields> 

코드 :

/// <summary> 
    /// Settings file which holds the name of the XML Fields 
    /// </summary> 
    public class setting1: ConfigurationSection 
    { 

     /// <summary> 
     /// Name of the setting1 Field 
     /// </summary> 
     [ConfigurationProperty("setting1", IsRequired = true)] 
     public String setting1 
     { 
      get 
      { 
       return (String)this["setting1"]; 
      } 
      set 
      { 
       this["setting1"] = value; 
      } 
     } 

     /// <summary> 
     /// Name of the setting2 Field 
     /// </summary> 
     [ConfigurationProperty("setting2",IsRequired = true)] 
     public String setting2 
     { 
      get 
      { 
       return (String)this["setting2"]; 
      } 
      set 
      { 
       this["setting2"] = value; 
      } 
     } 
} 
} 

내가이 일을하고

<CustomFields setting1='hello' setting2='world'/> 

어느 쪽이 작동하지 않습니다. 분명히 '속성'구문을 이해하지 못합니다.

내가 잘못하고있는 아이디어가 있습니까? 감사. 이 방법으로 필요한 속성을 정의 할 경우

+0

LoadValuesFromXml 메서드에 대한 코드를 게시 할 수 있습니까? 사용자 지정 섹션의 정보가 들어있는 XmlNode에서 값을 검색하는 방식으로 문제가 발생할 수 있습니다. –

+0

구성 설정을 확장하려고합니다. 해당 코드를 추가합니다. – Damien

답변

1

: 각 "속성"이 아닌

<CustomFields> 
    <property name="setting1">hello</property> 
    <property name="setting2">world</property> 
    ... 
</CustomFields> 

을 CustomFields에 대한 자식 노드가되어 당신의 재산은 이제 그 자식 노드의 CustomFields 아닌 속성에 대해/값 속성입니다 첫 번째 예와 마찬가지로 노드.

당신이 속성을 많이 가지고 있고 여기에 더 우아를 설정하려는 경우 고려해 볼 수 있습니다 두 가지 옵션이 있습니다 :

:

1)이 약간 변경된 사용자 정의 섹션()에 대해 다음과 같은 구조를 사용하여이

<CustomFields> 
    <setting1 value="hello"/> 
    <setting2 value="world"/> 
    ... 
</CustomFields> 
과 값을 검색하는 데 사용되는 속성을 정의하려면 다음 코드 :

public class CustomFields: ConfigurationSection 
{    
    [ConfigurationProperty("setting1")] 
    public PropertyElement Setting1 
    { 
     get 
     { 
      return (PropertyElement)this["setting1"]; 
     } 
     set 
      { this["setting1"] = value; } 
    } 

    [ConfigurationProperty("setting2")] 
    public PropertyElement Setting2 
    { 
     get 
     { 
      return (PropertyElement)this["setting2"]; 
     } 
     set 
      { this["setting2"] = value; } 
    } 
} 
public class PropertyElement : ConfigurationElement 
{ 
    [ConfigurationProperty("value", IsRequired = false)] 
    public String Value 
    { 
     get 
     { 
      return (String)this["value"]; 
     } 
     set 
     { 
      this["value"] = value; 
     } 
    } 
} 

T 암탉은 값을 검색하기 : 자세한 내용은

string setting1value = myCustomFields.Setting1.Value; 
string setting2value = myCustomFields.Setting2.Value; 

은 MSDN에 How to: Create Custom Configuration Sections Using ConfigurationSection를 참조하십시오.

2) 속성 및 리플렉션에 의존하지 않고 프로그래밍 방식으로 접근하십시오. 이 경우에는 ConfigurationSection class 또는 IConfigurationSectionHandler을 사용할 수 있습니다. 결과적으로 코드에서 사용자 정의 섹션 데이터가 포함 된 XML 노드에 액세스 할 수 있으며 모든 종류의 XML 구조를로드 할 수 있습니다.

+0

건배! 그게 내가 찾고 있던거야. – Damien

+0

당신은 환영합니다 :) –