2012-12-12 5 views
3

.NET 구성 API를 사용하여 사용자 지정 구성 섹션을 작성하고 있습니다. 두 개의 하위 요소 중 하나를 정확하게 포함 할 수있는 섹션을 정의하고 싶습니다.어떻게 상호 배타적 인 사용자 정의 구성 요소를 설정합니까?

예 : 현재

<MyCustomSection> 
    <myItems> 
     <myItemType name="1"> 
      <firstSubTypeConfig /> 
     </myItemType> 
     <myItemType name="2"> 
      <secondSubTypeConfig /> 
     </myItemType> 
    </myItems> 
</MyCustomSection> 

, 나는이 부분은 다음과 같이 정의했다 :

public class MyCustomSection : ConfigurationSection 
{ 
    public override bool IsReadOnly() 
    { 
     return false; 
    } 

    [ConfigurationProperty("myItems")] 
    public MyItemsCollection MyItems 
    { 
     get { return (MyItemsCollection)base["myItems"]; } 
     set { base["myItems"] = value; } 
    } 
} 

[ConfigurationCollection(typeof(MyItemType), AddItemName="myItemType", 
    CollectionType=ConfigurationElementCollectionType.BasicMap)] 
public class MyItemsCollection : ConfigurationElementCollection 
{ 
    public override bool IsReadOnly() 
    { 
     return false; 
    } 

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

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

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

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return (element as MyItemType).Name; 
    } 
} 


public class MyItemType : ConfigurationElement 
{ 
    public override bool IsReadOnly() 
    { 
     return false; 
    } 

    [ConfigurationProperty("name", IsRequired=true)] 
    public string Name 
    { 
     get { return (string)base["name"]; } 
     set { base["name"] = value; } 
    } 

    [ConfigurationProperty("firstSubTypeConfig")] 
    public FirstSubTypeConfig FirstSubTypeConfig 
    { 
     get { return (FirstSubTypeConfig)base["firstSubTypeConfig"]; } 
     set { base["firstSubTypeConfig"] = value; } 

    } 

    [ConfigurationProperty("secondSubTypeConfig")] 
    public SecondSubTypeConfig SecondSubTypeConfig 
    { 
     get { return (SecondSubTypeConfig)base["secondSubTypeConfig"]; } 
     set { base["secondSubTypeConfig"] = value; } 
    } 
} 

public class FirstSubTypeConfig : ConfigurationElement 
{ 
    public override bool IsReadOnly() 
    { 
     return false; 
    } 
} 

public class SecondSubTypeConfig : ConfigurationElement 
{ 
    public override bool IsReadOnly() 
    { 
     return false; 
    } 
} 

구성은 난 단지 지정하는 경우에도 secondSubTypeConfig 요소를 추가 할 구성 섹션을 저장, 현재도 수정 프로그램 저장되며, firstSubTypeConfig 요소는 입니다.

내 첫 번째 생각은 FirstSubTypeConfigSecondSubTypeConfig에 대한 공통 기본 클래스를 소개하는 것이지만 구성 API가이를 처리 할 수 ​​있는지 또는 어떻게 처리할지 명확하지 않습니다.

어떻게 상호 배타적 인 사용자 정의 구성 요소를 설정합니까?

+0

예는 너무 추상적입니다. 당신이 실제로하고 싶은 것에 더 가깝도록 업데이트하면 당신을 도울 수 있습니다. 이 시점에서 그것은 너무 모호하여 모든 접근법을 다루기 위해 페이지를 작성해야하고 트랙을 벗어나 많은 시간을 낭비 할 위험이 크다. 이름, 유형 (특히 두 개의 "부속 유형")을 갱신하고 유형을 어떻게 사용하는지 설명하십시오. 다른 "SubTypeconfig"클래스가 있어야합니까? 그들의 둘 이상이 될 것입니까? 얼마나 많은 "myItemType"이있을 것입니까? 동일한 "SubTypeconfig"를 공유 할 수 있습니까? –

답변

1

"올바른"접근 방식인지는 확실하지 않지만 작동합니다.

질문에 작성된 코드는 설정 파일을 올바르게로드합니다. ElementInformation.IsPresent 특성을 확인하여 정확히 하나의 요소가 포함되었는지 확인할 수 있습니다.

는 config 저장에 관해서는

var custom = (MyCustomSection)ConfigurationManager.GetSection("MyCustomSection"); 
foreach (MyItemType itemType in custom.MyItems) 
{ 
    if (itemType.FirstSubTypeConfig.ElementInformation.IsPresent 
     && itemType.SecondSubTypeConfig.ElementInformation.IsPresent) 
    { 
     throw new ConfigurationErrorsException("At most one of firstSubTypeConfig or secondSubTypeConfig can be specified in a myItemType element"); 
    } 
    else if (!itemType.FirstSubTypeConfig.ElementInformation.IsPresent 
     && !itemType.SecondSubTypeConfig.ElementInformation.Ispresent) 
    { 
     throw new ConfigurationErrorsException("Either a firstSubTypeConfig or a secondSubTypeConfig element must be specified in a myItemType element"); 
    } 
} 

, null로 설정 ElementInformation.IsPresent 확인하고 명시 적으로하면 config 파일에 기록되는 요소를 방지 할 것으로 보인다. 예 :

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
var custom = (MyCustomSection)config.GetSection("MyCustomSection"); 

//make modifications against the custom variable ... 

foreach (MyItemType itemType in custom.MyItems) 
{ 
    if (!itemType.FirstSubTypeConfig.ElementInformation.IsPresent) 
     itemType.FirstSubTypeConfig = null; 
    if (!itemType.SecondSubTypeConfig.ElementInformation.IsPresent) 
     itemType.SecondSubTypeConfig = null; 
} 

config.Save(); 
관련 문제