2011-09-19 2 views
1

꽤 큰 구성의 앱이 있습니다. 각 매개 변수의 모든 구성 섹션은 .Net ConfigurationProperty 특성으로 정의되며 모두 DefaultValue 속성을 갖습니다.속성이있는 클래스에 대해 생성 된 ConfigurationPropertyAttribute에 액세스하는 방법 ConfigurationProperty

우리 제품은 한 국가의 고객이나 국가 간에도 많이 사용자 지정할 수 있으므로 Configurator.exe를 사용하면 큰 구성 파일을 편집 할 수 있습니다.

이 Configurator.exe에서 정의 된 많은 많은 DefaultValue 속성에 액세스 할 수 있으면 정말 멋지 겠지만 ... 그러나 나는 어떻게 그 것에 액세스 할 수 있는지에 대한 단일 아이디어가 없습니다. 속성에 의해 생성 된 속성.

예컨대 :

public class MyCollection : ConfigurationElementCollection 
{ 
    public MyCollection() 
    { 
    } 

    [ConfigurationProperty(MyAttr,IsRequired=false,DefaultValue=WantedValue)] 
    public MyAttributeType MyAttribute 
    { 
     //... property implementation 
    } 
} 

내가 프로그래밍 값 WantedValue에 액세스하는 데 가장 일반적으로 가능하다 필요한 것은. (그렇지 않으면 정의 된 모든 ConfigSection을 수동으로 찾아보고 각 필드의 DefaultValues를 수집 한 다음 구성자가이 값을 사용하는지 확인하십시오.)

멋진 모양으로 보일 것입니다 : MyCollection.GetListConfigurationProperty() 이는 ConfigurationPropertyAttribute 이름, IsRequired, IsKey, IsDefaultCollection 및 을 호출 할 수있는 개체 DefaultValue

어떤 아이디어가 있습니까?

내가 ConfigSection 형, 내가 원하는 분야의 만들면 기본적 값의 유형, 그리고 문자열 값으로 공급 :

답변

0

이 내가하고 싶었던 것에 성공하는 달성하기 위해 일어난 클래스입니다 내가 원하는 분야.

public class ExtensionConfigurationElement<TConfigSection, UDefaultValue> 
    where UDefaultValue : new() 
    where TConfigSection : ConfigurationElement, new() 
{ 
    public UDefaultValue GetDefaultValue(string strField) 
    { 
     TConfigSection tConfigSection = new TConfigSection(); 
     ConfigurationElement configElement = tConfigSection as ConfigurationElement; 
     if (configElement == null) 
     { 
      // not a config section 
      System.Diagnostics.Debug.Assert(false); 
      return default(UDefaultValue); 
     } 

     ElementInformation elementInfo = configElement.ElementInformation; 

     var varTest = elementInfo.Properties; 
     foreach (var item in varTest) 
     { 
      PropertyInformation propertyInformation = item as PropertyInformation; 
      if (propertyInformation == null || propertyInformation.Name != strField) 
      { 
       continue; 
      } 

      try 
      { 
       UDefaultValue defaultValue = (UDefaultValue) propertyInformation.DefaultValue; 
       return defaultValue; 
      } 
      catch (Exception ex) 
      { 
       // default value of the wrong type 
       System.Diagnostics.Debug.Assert(false); 
       return default(UDefaultValue);     
      } 
     } 

     return default(UDefaultValue); 
    } 
}