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

「使用されていない」要素を取り除くために構成セクションをうまく折りたたむ方法を教えてもらえますか?

4

1 に答える 1

1

.NET構成がコレクションをシリアル化/逆シリアル化する方法であるため、1行から2行のコードを追加するだけでそれを行う方法がわかりません。1 つの解決策はおそらく - 独自の型コンバーターを作成する - の子孫ですConfigurationConverterBaseAutoSyncConfigurationそれをあなたの財産に適用してください。コンバーターでは、コレクションをシリアル化/逆シリアル化するために何でもします。

于 2013-02-05T19:16:47.910 に答える