Skip to content

Instantly share code, notes, and snippets.

@imzjy
Created May 13, 2019 08:36
Show Gist options
  • Save imzjy/9e7ba85c0e7943966f7dd2b34a8f874d to your computer and use it in GitHub Desktop.
Save imzjy/9e7ba85c0e7943966f7dd2b34a8f874d to your computer and use it in GitHub Desktop.
dynamic load the DLL that implements the IOrderRule interfaces. http://www.jeremybytes.com/Demos.aspx#PR
using OrderRules.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace OrderRules.RuleChecker
{
public static class DynamicOrderRuleLoader
{
public static List<IOrderRule> LoadRules(string assemblyPath)
{
var ruleCatalog = new List<IOrderRule>();
if (!Directory.Exists(assemblyPath))
return ruleCatalog;
IEnumerable<string> assemblyFiles = Directory.EnumerateFiles(
assemblyPath, "*.dll", SearchOption.TopDirectoryOnly);
foreach(string assemblyFile in assemblyFiles)
{
Assembly assembly = Assembly.LoadFrom(assemblyFile);
foreach(Type type in assembly.ExportedTypes)
{
if(type.IsClass &&
typeof(IOrderRule).IsAssignableFrom(type))
{
IOrderRule rule = Activator.CreateInstance(type) as IOrderRule;
ruleCatalog.Add(rule);
}
}
}
return ruleCatalog;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment