Skip to content

Instantly share code, notes, and snippets.

@gscattolin
Created September 10, 2013 18:02
Show Gist options
  • Select an option

  • Save gscattolin/6513144 to your computer and use it in GitHub Desktop.

Select an option

Save gscattolin/6513144 to your computer and use it in GitHub Desktop.
Plugin
namespace DBB.Plugins.Interfaces
{
public interface IPlugin
{
/// <summary>
/// Gets or sets the plugin's name.
/// </summary>
string Name { get; }
/// <summary>
/// Gets or sets the plugin's version.
/// </summary>
string Version { get; }
/// <summary>
/// Runs the plugin's main method.
/// </summary>
/// <returns>Some object.</returns>
object Run();
}
}
/// <summary>
/// Gets a list of Dll files within a specified path.
/// </summary>
/// <param name="path">Path to retrieve Dll files in.</param>
/// <returns>List of strings with file names.</returns>
public static List<string> GetDllList(string path)
{
return new List<String>(Directory.GetFiles(path, "*.dll"));
}
/// <summary>
/// Gets a list of IPlugin objects.
/// </summary>
/// <param name="dllFiles">List of Dll files to check for plugin capabilities.</param>
/// <returns>List of IPlugin objects.</returns>
public static List<IPlugin> GetPlugins(List<string> dllFiles)
{
List<IPlugin> retVal = new List<IPlugin>();
foreach (string dll in dllFiles)
{
try
{
// Load dll.
Assembly assembly = Assembly.LoadFile(dll);
// Get class names.
Type[] types = assembly.GetTypes();
// Add plugin classes to plugin list.
foreach (Type cls in types)
{
if (cls.IsPublic)
{
// Get classe's implemented interfaces.
Type[] interfaces = cls.GetInterfaces();
foreach (Type iface in interfaces)
{
// Is current interface IPlugin?
if (cls.GetInterface(iface.FullName) == typeof(IPlugin))
retVal.Add(assembly.CreateInstance(cls.FullName) as IPlugin);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
return retVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment