2010-02-15 3 views
0

아래 코드를 붙여 넣으면 오류가 발생합니다.C#에서 ConfigurationElement를 구현하는 방법은 무엇입니까?

클래스 ZDRCreatorTests.ZDRCreatorTests의 인스턴스를 만들 수 없습니다. 오류 : System.Configuration.ConfigurationErrorsException : 'indexedFolder'속성의 기본값을 파싱 할 수 없습니다. 오류 : 재산 'indexedFolder'유형 '의 DirectoryInfo'에 대한 문자열로/변환을 지원하는 컨버터를 찾을 수 없습니다 ..

namespace ZDRCreator 
{ 
    public class ZDRCreatorElement : ConfigurationElement 
    { 
     // Create the element. 
     public ZDRCreatorElement() 
     { } 

     // Get or set the IndexedFolder 
     [ConfigurationProperty("indexedFolder", DefaultValue = "", IsRequired = true)] 
     public DirectoryInfo IndexedFolder { 
      get { return (DirectoryInfo)this["indexedFolder"]; } 
      set { this["indexedFolder"] = value; } 
     } 

     // Get or set the OutputFolder 
     [ConfigurationProperty("outputFolder", DefaultValue = "", IsRequired = true)] 
     public DirectoryInfo OutputFolder { 
      get { return (DirectoryInfo)this["outputFolder"]; } 
      set { this["outputFolder"] = value; } 
     } 

     // Get or set the ZDRFile 
     [ConfigurationProperty("ZDRFile", DefaultValue = "", IsRequired = true)] 
     public FileInfo ZDRFile { 
      get { return (FileInfo)this["ZDRFile"]; } 
      set { this["ZDRFile"] = value; } 
     } 

     // Get or set the overwriteOutput flag 
     [ConfigurationProperty("overwriteOutput", DefaultValue = "false", IsRequired = true)] 
     public bool OverwriteOutput { 
      get { return (bool)this["overwriteOutput"]; } 
      set { this["overwriteOutput"] = value; } 
     } 

     // Get or set the OutputFile 
     [ConfigurationProperty("outputFile", DefaultValue = "", IsRequired = true)] 
     public String OutputFile { 
      get { return (String)this["outputFile"]; } 
      set { this["outputFile"] = value; } 
     } 
     // Get or set the OutputFile 
     [ConfigurationProperty("pathMask", DefaultValue = "", IsRequired = true)] 
     public String PathMask { 
      get { return (String)this["pathMask"]; } 
      set { this["pathMask"] = value; } 
     } 
    } 
} 

내가 문자열을 넣어려고하기 때문에 오류가 실현 DirectoryInfo 객체에서. 내 질문은 이것입니다 : XML에서 읽은 문자열이나 기본 데이터 형식 만 저장 한 다음 xml을 읽은 다음 다른 개체로 변환한다고 가정합니까? 또는, 내가 내부적으로 사용할 대상으로 그들을 구성 할 수있는 곳이 있습니까? 입력에 대한 유효성 확인은 어디에서 발생합니까?

답변

1

이 질문에 대한 답변이 없지만 은 Configuration Section Designer project on CodePlex입니다.

응용 프로그램의 구성 섹션에 대해 디자인 타임 환경을 제공하고 디자이너의 클래스 코드를 생성하고 구성 파일에 넣기위한 템플릿도 제공합니다.

이 모든 작업을 손으로 직접 수행해야하는 것은 매우 지루하고 구성 섹션 디자이너 이 처리하지 않는 상황을 아직 보지 못했습니다..

+0

당신이 말한 것은 모두 사실입니다. CSD는 놀라운 도구이지만 요즘에는 Visual Studio의 정식 버전입니다. 너무 슬다. – QueueHammer

3

을 추가 할 수 있습니다.이 변환기는 DirectoryInfo와 /로부터 Directory로 변환되는 문자열을 변환합니다. 변환기는 TypeConverter에서 파생 된 클래스입니다.

[ConfigurationProperty("ZDRFile", DefaultValue = "", IsRequired = true)] 
[TypeConverter(typeof(YourCustomFileInfoTypeConverter))] 
public FileInfo ZDRFile { 
    get { return (FileInfo)this["ZDRFile"]; } 
    set { this["ZDRFile"] = value; } 
} 
+1

나는 당신의 대답을 선호했다. 그리고 나중에 비틀 거린 사람들을 위해 MSDN 문서에서 사용자 지정 형식 변환기를 만드는 과정을 안내합니다. http://msdn.microsoft.com/en-us/library/ayybcxe5.aspx –

관련 문제