1

最初のカスタム構成セクションを作成しましたが、すべてうまくいきました。それから私はそれを伸ばしに行きました、そしてそれは横に行きました。私が抱えている問題と多くの例を見つけることができないのは、私のセクション内に2つの異なるタイプのコレクションが必要なことです。両方のコレクションの要素は完全に異なりますが、それぞれ同じです。エラーが発生したため、構成セクションで正しいコレクションを返すことができません。チートして2つのセクションを作成することはできますが、これを実装する正しい方法ではないようです。

okいくつかのコード

<configSections>
  <section name="MyFileSection" type="My.ConfigManager.MyFileListConfiguration, MyConfigManager" />
</configSections>
<MyFileSection>
  <MyDirectoryRootCollection>
    <add rootName="MyDataLocation"   rootLocation="\\MyServer\MyDirectory"/>
    <add rootName="YourDataLocation" rootLocation="\\YourServer\YourDirectory"/>
  </MyDirectoryRootCollection>
  <MyFileListCollection>
    <add keyName="MyFile1"   copyType="File" sourceFileName="TestFile1" />
    <add keyName="MyFile2"   copyType="FTP"  sourceFileName="TestFile2" />
    <add keyName="MyFile3"   copyType="File" sourceFileName="TestFile3" />
  </MyFileListCollection>
</MyFileSection>

つまり、MyFileSection内には、MyDirectoryRootCollectionとMyFileListCollectionの2つのコレクションがあります。1つのConfigクラスで、このように2つを取得できると思いました。

namespace My.ConfigManager
{
    public class MyFileListConfiguration : ConfigurationSection
    {
        private static string sMyFileListCollectionConst      = "MyFileListCollection";
        private static string sMyDirectoryRootCollectionConst = "MyDirectoryRootCollection";


        [ConfigurationProperty("MyFileListCollection", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(MyFileListConfigEleCollection), AddItemName = "MyFileListCollection")]
        public MyFileListConfigEleCollection MyFileListCollection
        {
            get { return ((MyFileListConfigEleCollection)(base["MyFileListCollection"])); }
        }

        [ConfigurationProperty("MyDirectoryRootCollection", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(MyDirectoryRootConfigEleCollection), AddItemName = "MyFileListCollection")]
        public MyFileListConfiguration MyDirectoryRoot
        {
            get { return ((MyFileListConfiguration)(base["MyDirectoryRootCollection"])); }
        }

    }

この呼び出しで構成セクションを取得しようとします

MyFileListConfiguration fileListSection = (MyFileListConfiguration)ConfigurationManager.GetSection("MyFileSection");

しかし、私はこのエラーを受け取ります-

System.Configuration.ConfigurationErrorsExceptionは未処理でした
Message="構成プロパティ'MyDirectoryRootCollection'はConfigurationSectionから派生していない可能性があります。"

私が間違っていることを誰かが考えていますか?セクション内に2つのまったく異なるコレクションを含めることは可能ですか?

4

2 に答える 2

3

これはうまくいくはずです。

使用法:

MyFileListConfiguration myConfig = (MyFileListConfiguration)ConfigurationManager.GetSection("MyFileSection");

myConfig.Files[0] or myConfig.Files["MyFile1"] => .KeyName / .CopyType / .SourceFileName
        //  myConfig.Directories[0] or myConfig.Directories["MyDataLocation"] => .RootName / .RootLocation


public class MyFileListConfiguration : ConfigurationSection
{
    [ConfigurationProperty("MyFileListCollection", IsDefaultCollection = false)]
    public MyFileListCollection Files
    {
        get { return ((MyFileListCollection)(base["MyFileListCollection"])); }
    }

    [ConfigurationProperty("MyDirectoryRootCollection", IsDefaultCollection = false)]
    public MyDirCollection Directories
    {
        get { return ((MyDirCollection)(base["MyDirectoryRootCollection"])); }
    }
}

[ConfigurationCollection(typeof(MyFileElement))]
public class MyFileListCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new MyFileElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((MyFileElement)(element)).KeyName;
    }

    /// <summary>
    /// Access the collection by index
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public MyFileElement this[int index]
    {
        get { return (MyFileElement)BaseGet(index); }
    }

    /// <summary>
    /// Access the collection by key name
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public new MyFileElement this[string key]
    {
        get { return (MyFileElement)BaseGet(key); }
    }
}

[ConfigurationCollection(typeof(MyDirElement))]
public class MyDirCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new MyDirElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((MyDirElement)(element)).RootName;
    }

    /// <summary>
    /// Access the collection by index
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public MyDirElement this[int index]
    {
        get { return (MyDirElement)BaseGet(index); }
    }

    /// <summary>
    /// Access the collection by key name
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public new MyDirElement this[string key]
    {
        get { return (MyDirElement)BaseGet(key); }
    }
}

public class MyFileElement : ConfigurationElement
{
    [ConfigurationProperty("keyName", DefaultValue = "", IsKey = true, IsRequired = true)]
    public string KeyName
    {
        get { return ((string)(base["keyName"])); }
        set { base["keyName"] = value; }
    }

    [ConfigurationProperty("copyType", DefaultValue = "")]
    public string CopyType
    {
        get { return ((string)(base["copyType"])); }
        set { base["copyType"] = value; }
    }

    [ConfigurationProperty("sourceFileName", DefaultValue = "")]
    public string SourceFileName
    {
        get { return ((string)(base["sourceFileName"])); }
        set { base["sourceFileName"] = value; }
    }
}

public class MyDirElement : ConfigurationElement
{
    [ConfigurationProperty("rootName", DefaultValue = "", IsKey = true, IsRequired = true)]
    public string RootName
    {
        get { return ((string)(base["rootName"])); }
        set { base["rootName"] = value; }
    }

    [ConfigurationProperty("rootLocation", DefaultValue = "")]
    public string RootLocation
    {
        get { return ((string)(base["rootLocation"])); }
        set { base["rootLocation"] = value; }
    }
}
于 2012-12-05T02:35:07.820 に答える
0

MyDirectoryRoot プロパティのタイプは、MyFileListConfiguration ではなく、MyDirectoryRootConfigEleCollection にする必要があります。add 要素名は、サンプルhttp://msdn.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.configurationcollectionattribute.aspxと一致するように「add」にする必要があることに注意してください。

于 2012-12-05T02:31:36.040 に答える