13

これが私が持っていたアイデアです:

小さな実行可能ファイルに、sectionGroup「applicationSettings」の下にある複数のセクションを含む app.config ファイルが必要です (「appSettings」ではなく、ファイルに書き込む必要はありません)。各セクションには、設定されている場合にロードする必要があるモジュールに対応する名前があります。

次に例を示します。

   <configuration>
     <configSections>
       <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
         <section name="Executable" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
         <section name="FirstModule" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
       </sectionGroup>
     </configSections>
     <applicationSettings>
       <Executable>
         <setting name="MyFirstSetting" serializeAs="String">
           <value>My awesome feature setting</value>
         </setting>
       </Executable>
       <FirstModule path="path to the modules assembly">
         <setting name="ImportantSettingToTheModule" serializeAs="String">
           <value>Some important string</value>
         </setting>
       </FirstModule>
     </applicationSettings>
   </configuration>

ここで FirstModule セクションを定義すると、アプリケーションでそのアセンブリをロードできるようになります。セクションが定義されていない場合、モジュールはロードされません。これは、1 つのモジュールだけでなく、まだ定義されていない数のモジュールにも当てはまります。

したがって、基本的には、実行時に定義されたセクションについて調べる必要があります。どうすればいいですか?

さらに、.NET 2.0 と下位互換性のある移植可能な実行可能ファイル (= Mono でも実行する必要があります) にしたいと考えています。

GitHub のプロジェクトを見てみるのも面白いかもしれません (現在はこのコミットにあります)。

4

1 に答える 1

24

ConfigurationManager.OpenExeConfiguration構成ファイルにロードする関数を見てください。

次に、返されるSystem.Configuration.ConfigurationクラスConfigurationManager.OpenExeConfigurationで、SectionGroupsプロパティを確認する必要があります。ConfigurationSectionGroupCollectionこれにより、セクションが見つかるa が返されますapplicationSettings

には、およびオブジェクトを含むプロパティがConfigurationSectionGroupCollectionあります。SectionsExecutableFirstModule ConfigurationSection

var config = ConfigurationManager.OpenExeConfiguration(pathToExecutable);
var applicationSettingSectionGroup = config.SectionGroups["applicationSettings"];
var executableSection = applicationSettingSectionGroup.Sections["Executable"];
var firstModuleSection = applicationSettingSectionGroup.Sections["FirstModule"];

オブジェクトまたはオブジェクトnullを取得した後に確認する必要があります。それらが null の場合、構成ファイルには存在しません。ConfigurationSectionGroupCollectionConfigurationSection

を使用してセクションを取得することもできますConfigurationManager.GetSection

var executableSection = (ClientSettingsSection)ConfigurationManager
    .GetSection("applicationSettings/Executable");
var firstModuleSection = (ClientSettingsSection)ConfigurationManager
    .GetSection("applicationSettings/FirstModule");

繰り返しますが、オブジェクトが存在する場合、nullそれらは構成ファイルに存在しません。

すべてのセクション名とグループのリストを取得するには、次のようにします。

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// use the line below instead if you want to load an app.config for a
//   different application other than the one the code is running in
// var config = ConfigurationManager.OpenExeConfiguration(pathToExecutable);

var names = new List<string>();
foreach (ConfigurationSectionGroup csg in config.SectionGroups)
    names.AddRange(GetNames(csg));

foreach (ConfigurationSection cs in config.Sections)
    names.Add(cs.SectionInformation.SectionName);

private static List<string> GetNames(ConfigurationSectionGroup configSectionGroup)
{
    var names = new List<string>();

    foreach (ConfigurationSectionGroup csg in configSectionGroup.SectionGroups)
        names.AddRange(GetNames(csg));

    foreach(ConfigurationSection cs in configSectionGroup.Sections)
        names.Add(configSectionGroup.SectionGroupName + "/" + cs.SectionInformation.SectionName);

    return names;
}
于 2013-02-19T18:19:05.310 に答える