2016-06-10 1 views
0

Stackoverflow에 대한 자습서를 따라 내 EXE의 구성 파일에 사용자 정의 구성 섹션을 추가했지만,이를 호출하면 null이 반환됩니다. 그것은 정적 생성자에 들어가기조차하지 않기 때문에 무언가가 명백하게 잘못되었지만 나는 무엇을 볼 수 없습니다.app.config에서 작동하도록 사용자 정의 구성 섹션을 가져올 수 없습니다.

여기 내 설정 파일과 찾을 섹션이 있습니다.

PresetFiltersConfiguration pf = (PresetFiltersConfiguration)ConfigurationManager.GetSection("PresetFilters"); 

을하고는 null를 돌려 심지어 내 클래스 또는 클래스 정적을 입력하지 않습니다

<configuration> 
    <appSettings> 
    </appSettings> 
     <configSections> 
      <section name="PresetFilters" type="ImageTool.PresetFiltersConfiguration, ImageTool" /> 
     </configSections> 
    <PresetFilters> 
    <add key="Default,-20,0,0,0,0" /> 
    <add key="No Change,0,0,0,0,0" /> 
    <add key="Dark Photo,10,10,0,0,-10" /> 
    </PresetFilters> 
</configuration> 

나는 다음과 같이 호출. 여기에 코드가 있습니다. 어떤 도움을 주시면 감사하겠습니다. 감사.

public class PresetFiltersConfiguration : ConfigurationSection 
{ 
    private static ConfigurationPropertyCollection properties; 
    private static ConfigurationProperty propPresets; 

    static PresetFiltersConfiguration() 
    { 
     propPresets = new ConfigurationProperty(null, typeof(PresetFiltersElementCollection), 
                 null, 
                 ConfigurationPropertyOptions.IsDefaultCollection); 
     properties = new ConfigurationPropertyCollection { propPresets }; 
    } 

    protected override ConfigurationPropertyCollection Properties 
    { 
     get 
     { 
      return properties; 
     } 
    } 

    public PresetFiltersElementCollection PresetFilter 
    { 
     get 
     { 
      return this[propPresets] as PresetFiltersElementCollection; 
     } 
    } 
} 

public class PresetFiltersElementCollection : ConfigurationElementCollection 
{ 
    public PresetFiltersElementCollection() 
    { 
     properties = new ConfigurationPropertyCollection(); 
    } 

    private static ConfigurationPropertyCollection properties; 

    protected override ConfigurationPropertyCollection Properties 
    { 
     get 
     { 
      return properties; 
     } 
    } 

    public override ConfigurationElementCollectionType CollectionType 
    { 
     get 
     { 
      return ConfigurationElementCollectionType.BasicMap; 
     } 
    } 

    protected override string ElementName 
    { 
     get 
     { 
      return "add"; 
     } 
    } 

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

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     var elm = element as PresetFiltersElement; 
     if (elm == null) throw new ArgumentNullException(); 
     return elm.KeyName; 
    } 
} 

public class PresetFiltersElement : ConfigurationElement 
{ 
    private static ConfigurationPropertyCollection properties; 
    private static ConfigurationProperty propKey; 

    protected override ConfigurationPropertyCollection Properties 
    { 
     get 
     { 
      return properties; 
     } 
    } 

    public PresetFiltersElement() 
    { 
     propKey = new ConfigurationProperty("key", typeof(string), 
                 null, 
                 ConfigurationPropertyOptions.IsKey); 
     properties = new ConfigurationPropertyCollection { propKey }; 
    } 

    public PresetFiltersElement(string keyName) 
     : this() 
    { 
     KeyName = keyName; 
    } 

    public string KeyName 
    { 
     get 
     { 
      return this[propKey] as string; 
     } 
     set 
     { 
      this[propKey] = value; 
     } 
    } 
} 
+0

configSection을 추가해야합니다. https://msdn.microsoft.com/en-us/library/2tw134k3.aspx –

+0

감사합니다. 편집 한대로 했는데도 아무 일도 일어나지 않습니다. –

+0

콘솔 응용 프로그램으로 이동하면 제대로 작동하지만이 클래스 라이브러리는 작동하지 않습니다. 그것은 잘 작동 config에서 다른 부분을 가지고 있지만 그것은 file.dll.config 파일을 읽고있다. –

답변

0

당신은 그렇지 않으면 응용 프로그램이 새 섹션을 처리하는 방법을 알 수 없습니다, 당신의 app.config에서이 같은 필요합니다.

<configSections> 
    <sectionGroup name="pageAppearanceGroup"> 
     <section 
     name="PresetFilters" 
     type="PresetFiltersConfiguration" 
     allowLocation="true" 
     allowDefinition="Everywhere" 
     /> 
    </sectionGroup> 
    </configSections> 
+0

고마워, (내 편집으로) 그래도 아무 일도 일어나지 않아. 그룹도 똑같이 시도 했어. –

+0

콘솔 응용 프로그램으로 이동하면 제대로 작동하지만이 클래스 라이브러리는 작동하지 않습니다. 그것은 잘 작동 config에서 다른 부분을 가지고 있지만 그것은 file.dll.config 파일을 읽고있다. –

0
이 일 수정 적용 후

하지만, 실제 오류가 클래스 라이브러리 작동하지 그것으로하는 것입니다, 나는 이것에 대한 새로운 질문을 만들었습니다.

감사합니다.

관련 문제