2012-12-12 44 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屬性以驗證是否包含一個元素。

例如

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"); 
    } 
} 

至於保存配置,似乎檢查ElementInformation.IsPresent,並明確將其設置爲null將阻止元素被寫入配置文件。例如

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(); 
相關問題