平々毎々(アーカイブ)

はてなダイアリーのアーカイブです。

app.config hacks

互いに無関係なセクションを並列に並べることはできる。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="fooSection" type="FooSection, FooAssembly"/>
    <section name="barSection" type="BarSection, BarAssembly"/>
  </configSections>

  <fooSection>
    <fooElement fff="fff" />
  </fooSection>

  <barSection>
    <bazElement baz="baz" />
  </barSection>
</configuration>

しかし、あるセクションの中に別のセクションを置くことはできない。(ConfigurationSectionを入れ子にできない。)
ConfigurationSectionはconfiguration要素の直接の子か、あるいはConfigurationGroupの直接の子でないといけないため。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="fooSection" type="FooSection, FooAssembly"/>
    <section name="barSection" type="BarSection, BarAssembly"/>
  </configSections>

  <fooSection>
    <fooElement fff="fff" />

    <!-- 以下がエラーになる -->
    <barSection>
      <bazElement baz="baz" />
    </barSection>

  </fooSection>
</configuration>

もしやりたければ、ConfigurationSectionを継承してBarSectionを作るのではなく、ConfigurationElementを継承してBarElementを作るしかない。
それでも、普通の方法では、FooSectionがBarElementに依存することになる。(FooAssemblyがBarAssemblyを参照しないといけない)

ConfigSectionを並列に並べるときのように、互いに無関係なままにはできない。

ただし、逆の依存関係なら、つまり、BarAssemblyがFooAssemblyを参照していいのなら、無理やりやろうと思えばできる。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="fooSection" type="FooSection, FooAssembly"/>
  </configSections>

  <fooSection>
    <fooElement fff="fff" />
    <childElement name="barElement" type="BarElement, BarAssembly">

      <!-- 以下の構成情報はBarAssemblyのBarElementが処理する -->
      <barElement>
        <bazElement baz="baz" />
      </barElement>

    </childElement>
  </fooSection>
</configuration>

(実装は次回以降。)