最初のカスタム構成セクションを作成しましたが、すべてうまくいきました。それから私はそれを伸ばしに行きました、そしてそれは横に行きました。私が抱えている問題と多くの例を見つけることができないのは、私のセクション内に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つのまったく異なるコレクションを含めることは可能ですか?