|
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
namespace DbKeeperNet.Engine
{
/// <summary>
/// Helper class which loads and executes Initialize() method of all
/// classes implementing IExtension interface.
/// Extension may be located also in external assemblies. In this
/// case is path absolute or relative to entry assembly.
/// If entry assembly can't be determined, path is relative to
/// this assembly.
/// </summary>
/// <see cref="IExtension"/>
public sealed class ExtensionLoader
{
/// <summary>
/// Load all configured assemblies with extension
/// within defined context.
/// </summary>
/// <param name="context">Context to which may be extensions registered.</param>
public static void LoadExtensions(IUpdateContext context)
{
if (context == null)
throw new ArgumentNullException("context");
foreach (ExtensionConfigurationElement e in DbKeeperNetConfigurationSection.Current.Extensions)
LoadExtensionsFromAssembly(context, e.Assembly);
}
private static void LoadExtensionsFromAssembly(IUpdateContext context, string assemblyName)
{
Assembly assembly = Assembly.Load(assemblyName);
Type[] assemblyTypes = assembly.GetExportedTypes();
Type interfaceType = typeof(IExtension);
foreach (Type type in assemblyTypes)
{
if ((interfaceType.IsAssignableFrom(type)) && (!type.IsInterface))
{
IExtension extension = (IExtension)Activator.CreateInstance(type);
extension.Initialize(context);
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm software developer since 1996. I started with assembler on Intel 8051 CPUs, during years I was interested in C, C++, Sybase PowerBuilder, PHP, Sybase Anywhere Database, MSSQL server and multiplatform development.
Currently I'm developing in C++ and C# (this is my favorit and I spent some time with MCPD achievement). I'm also interested in design patterns.