0

다음과 같이 해결되었습니다! - 사용자 정의 섹션이있는 개별 App.Config가있는 여러 개의 WPF 프로젝트가 있습니다. 모든 사용자 정의 섹션은 동일한 구조를가집니다.ConfigurationManager - 다른 App.config 구조가 다른 프로젝트 - 클래스 공유

한 프로젝트의 경우 ConfigurationManager를 사용하고 사용자 지정 ConfigurationSection, ConfigurationCollection, ConfigurationElement를 만들고 모든 것이 해당 프로젝트에서 올바르게 작동합니다.

그런 다음 클래스 라이브러리에서 사용자 지정 구성 클래스를 이동하여 모든 프로젝트에서 사용할 수 있도록했지만 이제는 프로젝트를 실행할 때 'System.TypeInitializationException'오류가 발생합니다. 이것은 이제 ConfigurationManager가 App을 찾을 수 없기 때문인 것으로 보입니다.

모든 프로젝트에서 클래스를 붙여 넣을 수는 있지만 제대로 작동하지만 그렇게하고 싶지는 않습니다. 나는 아마 명백한 것을 놓치고 있을지도 모른다. 어떤 도움이라도 대단히 감사합니다. 감사!!!

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Configuration; 

namespace WordAddinForms 
{ 

    public class CustomConfig : ConfigurationSection 
    { 
    public static readonly CustomConfig Settings = 
     (CustomConfig)ConfigurationManager.GetSection("custom-configuration"); 

    [ConfigurationProperty("activities")] 
    public ActivityElementCollection Activities 
    { 
     get { return (ActivityElementCollection)base["activities"]; } 
    }  
    } 

    [ConfigurationCollection(typeof(ActivityElement), AddItemName = "activity", 
    CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)] 
    public class ActivityElementCollection : ConfigurationElementCollection, IEnumerable<ActivityElement> 
    { 
    IEnumerator<ActivityElement> IEnumerable<ActivityElement>.GetEnumerator() 
    { 
     return this.OfType<ActivityElement>().GetEnumerator(); 
    } 
    public override ConfigurationElementCollectionType CollectionType 
    { 
     get { return ConfigurationElementCollectionType.BasicMap; } 
    } 
    protected override string ElementName 
    { 
     get { return "activity"; } 
    } 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new ActivityElement(); 
    } 
    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return (element as ActivityElement).Name; 
    } 

    public ActivityElement this[int index] 
    { 
     get { return (ActivityElement)base.BaseGet(index); } 
     set 
     { 
     if (base.BaseGet(index) != null) 
     { 
      base.BaseRemoveAt(index); 
     } 
     base.BaseAdd(index, value); 
     } 
    } 
    public ActivityElement this[string name] 
    { 
     get { return (ActivityElement)base.BaseGet(name); } 
    } 

    } 

    public class ActivityElement : ConfigurationElement 
    { 
    [ConfigurationProperty("name", DefaultValue = "String.Empty")] 
    public string Name 
    { 
     get { return (string)base["name"]; } 
    }   
    [ConfigurationProperty("location", DefaultValue = "String.Empty")] 
    public string Location 
    { 
     get { return (string)base["location"]; } 
    }  

    [ConfigurationProperty("files")] 
    public FileElementCollection Files 
    { 
     get { return (FileElementCollection)base["files"]; } 
    } 
    } 

    [ConfigurationCollection(typeof(FileElement), AddItemName = "file", 
    CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)] 
    public class FileElementCollection : ConfigurationElementCollection, IEnumerable<FileElement> 
    { 
    IEnumerator<FileElement> IEnumerable<FileElement>.GetEnumerator() 
    { 
     return this.OfType<FileElement>().GetEnumerator(); 
    } 
    public override ConfigurationElementCollectionType CollectionType 
    { 
     get { return ConfigurationElementCollectionType.BasicMap; } 
    } 
    protected override string ElementName 
    { 
     get { return "file"; } 
    } 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new FileElement(); 
    } 
    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return (element as FileElement).Name; 
    } 

    public FileElement this[int index] 
    { 
     get { return (FileElement)base.BaseGet(index); } 
     set 
     { 
     if (base.BaseGet(index) != null) 
     { 
      base.BaseRemoveAt(index); 
     } 
     base.BaseAdd(index, value); 
     } 
    } 
    public FileElement this[string name] 
    { 
     get { return (FileElement)base.BaseGet(name); } 
    } 
    } 

    public class FileElement : ConfigurationElement 
    { 
    [ConfigurationProperty("name", DefaultValue = "String.Empty")] 
    public string Name 
    { 
     get { return (string)base["name"]; } 
    } 

    /// <remarks /> 
    [ConfigurationProperty("location", DefaultValue = "String.Empty")] 
    public string Location 
    { 
     get { return (string)base["location"]; } 
    }   
    } 
} 

편집 - app.config 파일 -

<?xml version="1.0" ?> 
<custom-configuration> 
<activities> 
    <activity name="Activities" location=".\Activity\"> 
    <files> 
     <file name="Running" location=".Running\"/> 
     <file name="Sports" location=".Sports\"/> 
     <file name="Fun" location=".Fun\"/> 
     <file name="Exercise" location=".Exercise\"/>  
    </files> 
    </activity> 
</activities> 
</custom-configuration> 

질문 고쳐 -

1) 나는

위에서 언급 한 구조의 다양한 프로젝트에 대해 여러의 app.config를 가지고, 그래서

2) 위에 코드와 같이 사용자 지정 구성 클래스를 만들었습니다

개별 프로젝트에서 복사 - 붙여 넣기 대신 클래스를 다시 사용할 수 있도록 클래스 라이브러리 \ 공유 라이브러리에 넣어야합니다. 클래스를 공유 라이브러리에 넣으면 프로젝트를 다시 빌드 할 수 있지만 실행하면 실패합니다.

Answer - 분명히 기본을 올바르게 이해해야합니다. 코드를 클래스 라이브러리로 이동 한 후에 클래스의 네임 스페이스와 위치가 변경되면 그에 따라 app.config를 업데이트해야했습니다. 불편을 드려 죄송합니다. 기본적으로, 클래스가 다른 어셈블리에 속하기 때문에 섹션의 "유형"을 업데이트해야했습니다.

+0

코드를 공유하지 않으려면 최소한 스택 추적을 공유하십시오. –

+0

GAC 어셈블리가 아니면 .dll이 필요합니다. –

+0

죄송합니다. 위와 같이 클래스 라이브러리 \ 공유 라이브러리에 사용하려는 구성 코드를 추가했습니다. 전체 코드를 projectA의 일부로 포함하면 제대로 작동합니다. 즉, projectA의 CustomConfig.Settings.Categories [ "mycategory"]에 액세스 할 때 코드가 올바르게 작동합니다. 그러나 그것을 분리하여 클래스 라이브러리 (classLibraryA)에 넣으면 projectA에서 CustomConfig.Settings.Categories [ "mycategory"]를 사용할 수 없습니다. – ShipOfTheseus

답변

0

답변 - 분명히 기본을 올바르게 이해해야합니다. 코드를 클래스 라이브러리로 이동 한 후에 클래스의 네임 스페이스와 위치가 변경되면 그에 따라 app.config를 업데이트해야했습니다. 불편을 드려 죄송합니다. 기본적으로, 클래스가 다른 어셈블리에 속하기 때문에 섹션의 "유형"을 업데이트해야했습니다.