Created
May 13, 2019 08:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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