using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Chernobyl.Collections.Generic;
namespace Chernobyl.Extensions.Configuration
{
///
/// Extensions for Chernobyl settings and related types.
///
public static class Extensions
{
///
/// The default string that is applied to the front of all setting keys.
/// For example, in "Chernobyl.Plugins", "Chernobyl" is the root.
///
public static string RootKey = "Chernobyl";
///
/// The default string that is placed between each element in a settings
/// key. For example, in "Chernobyl.Plugins" the '.' is the separator.
///
public static string KeySeparator = ".";
///
/// The default string that is applied to any settings value that contains
/// the list of plugin names.
///
public static string PluginKey = RootKey + KeySeparator + "Plugins";
///
/// The default character placed between assembly names in the settings
/// identified by .
///
public static char PluginNameSeparator = ';';
///
/// Retrieves the of plugins identified by
/// in the configuration provided.
///
/// The configuration to look in for the plugin
/// names.
/// The names of the plugin assemblies from .
public static IEnumerable GetPluginNames(this global::System.Configuration.Configuration config)
{
return config.AppSettings.Settings[PluginKey].Value.EmptyIfNull()
.Split(PluginNameSeparator)
.Where(str => str.IsEmpty() == false)
.Select(str => new AssemblyName(str));
}
///
/// Retrieves the plugin instances identified by
/// in the configuration provided.
///
/// The configuration to look in for the plugin
/// names.
/// The instances from
/// .
public static IEnumerable GetPluginAssemblies(this global::System.Configuration.Configuration config)
{
return config.GetPluginNames().Select(Assembly.Load);
}
}
}