2013-02-05 21 views
2

我已經爲app.config中的自定義嵌套配置集合創建了自己的一組類。您可以在下面看到我必須使用的當前配置。我想知道的是如何修改我的類,以便不需要AutoSyncConfiguration和WatchedFolders元素。我想我得到的配置部分看起來像這樣:自定義配置部分 - 刪除不需要的收藏標籤

<custom> 
    <BackupLocation Name="S3" Details="AccessKey=asdf;SecretKey=asdf;BucketName=asdf"> 
    <WatchedFolder Name="test1" LocalFolder="C" RemoteFolder="Z" FileSpec="*"></WatchedFolder> 
    <WatchedFolder Name="test2" LocalFolder="D" RemoteFolder="X" FileSpec="*.doc"></WatchedFolder> 
    </BackupLocation> 
    <BackupLocation Name="External" Details="MappedDrive=X;"> 
    <WatchedFolder Name="test" LocalFolder="D" RemoteFolder="XPhotos" FileSpec="*.jpeg"></WatchedFolder> 
    </BackupLocation> 
</custom> 

下面是我的班,我的app.config的相關部分和代碼行以獲取自定義配置:

public class Custom : ConfigurationSection 
{ 
    [ConfigurationProperty("AutoSyncConfiguration")] 
    public BackupLocationElementCollection AutoSyncConfiguration 
    { 
     get { return this["AutoSyncConfiguration"] as BackupLocationElementCollection; } 
    } 
} 
public class BackupLocationElementCollection : ConfigurationElementCollection 
{ 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new BackupLocationElement(); 
    } 
    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((BackupLocationElement)element).Name; 
    } 
    public override ConfigurationElementCollectionType CollectionType 
    { 
     get { return ConfigurationElementCollectionType.BasicMap; } 
    } 
    protected override string ElementName 
    { 
     get { return "BackupLocation"; } 
    } 
    public BackupLocationElement this[int index] 
    { 
     get { return (BackupLocationElement)BaseGet(index); } 
     set 
     { 
      if (BaseGet(index) != null) 
      { 
       BaseRemoveAt(index); 
      } 
      BaseAdd(index, value); 
     } 
    } 
    new public BackupLocationElement this[string backupName] 
    { 
     get { return (BackupLocationElement)BaseGet(backupName); } 
    } 
    public bool ContainsKey(string key) 
    { 
     bool result = false; 
     object[] keys = BaseGetAllKeys(); 
     foreach (object obj in keys) 
     { 
      if ((string)obj == key) 
      { 
       result = true; 
       break; 
      } 
     } 
     return result; 
    } 
} 

public class BackupLocationElement : ConfigurationElement 
{ 
    [ConfigurationProperty("Name", IsRequired = true, IsKey = true)] 
    public string Name 
    { 
     get { return this["Name"] as string; } 
     set { this["Name"] = value; } 
    } 

    [ConfigurationProperty("Details", IsRequired = true, IsKey = false)] 
    public string Details 
    { 
     get { return this["Details"] as string; } 
     set { this["Details"] = value; } 
    } 

    [ConfigurationProperty("WatchedFolders")] 
    public WatchedFolderElementCollection WatchedFolders 
    { 
     get { return this["WatchedFolders"] as WatchedFolderElementCollection; } 
    } 
} 

public class WatchedFolderElementCollection : ConfigurationElementCollection 
{ 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new WatchedFolderElement(); 
    } 
    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((WatchedFolderElement)element).Name; 
    } 
    public override ConfigurationElementCollectionType CollectionType 
    { 
     get { return ConfigurationElementCollectionType.BasicMap; } 
    } 
    protected override string ElementName 
    { 
     get { return "WatchedFolder"; } 
    } 
    public WatchedFolderElement this[int index] 
    { 
     get { return (WatchedFolderElement)BaseGet(index); } 
     set 
     { 
      if (BaseGet(index) != null) 
      { 
       BaseRemoveAt(index); 
      } 
      BaseAdd(index, value); 
     } 
    } 
    new public WatchedFolderElement this[string folderName] 
    { 
     get { return (WatchedFolderElement)BaseGet(folderName); } 
    } 
    public bool ContainsKey(string key) 
    { 
     bool result = false; 
     object[] keys = BaseGetAllKeys(); 
     foreach (object obj in keys) 
     { 
      if ((string)obj == key) 
      { 
       result = true; 
       break; 
      } 
     } 
     return result; 
    } 
} 

public class WatchedFolderElement : ConfigurationElement 
{ 
    [ConfigurationProperty("Name", IsRequired = true, IsKey = true)] 
    public string Name 
    { 
     get { return this["Name"] as string; } 
     set { this["Name"] = value; } 
    } 

    [ConfigurationProperty("LocalFolder", IsRequired = true, IsKey = false)] 
    public string LocalFolder 
    { 
     get { return this["LocalFolder"] as string; } 
     set { this["LocalFolder"] = value; } 
    } 

    [ConfigurationProperty("RemoteFolder", IsRequired = true, IsKey = false)] 
    public string RemoteFolder 
    { 
     get { return this["RemoteFolder"] as string; } 
     set { this["RemoteFolder"] = value; } 
    } 

    [ConfigurationProperty("FileSpec", IsRequired = true, IsKey = false)] 
    public string FileSpec 
    { 
     get { return this["FileSpec"] as string; } 
     set { this["FileSpec"] = value; } 
    } 
} 

以下是我的app.config:

<configuration> 
    <configSections> 
    <section name="custom" type="AutoSync.Custom, AutoSync" /> 
    </configSections> 

    <custom> 
    <AutoSyncConfiguration> 
     <BackupLocation Name="S3" Details="AccessKey=asdf;SecretKey=asdf;BucketName=asdf"> 
     <WatchedFolders> 
      <WatchedFolder Name="test1" LocalFolder="C" RemoteFolder="Z" FileSpec="*"/> 
     </WatchedFolders> 
     </BackupLocation> 
     <BackupLocation Name="External" Details="MappedDrive=X;"> 
     <WatchedFolders> 
      <WatchedFolder Name="test" LocalFolder="D" RemoteFolder="XPhotos" FileSpec="*.jpeg" /> 
     </WatchedFolders> 
     </BackupLocation> 
    </AutoSyncConfiguration> 
    </custom> 
</configuration> 

我的代碼如下:

Custom config = (Custom)ConfigurationManager.GetSection("custom"); 

有人能告訴我如何成功摺疊我的配置部分以擺脫「未使用」的元素?

回答

1

我不明白如何通過簡單地添加一兩行代碼來完成,因爲它只是如何.NET配置序列化/反序列化集合。一種解決方案可能是 - 創建你自己的類型轉換器 - ConfigurationConverterBase的後代。將其應用於您的AutoSyncConfiguration屬性。在轉換器中做任何你想要的序列化/反序列化集合。