Created
September 19, 2020 01:30
-
-
Save k4m4r82/d6b0bae79d491ff87e363482ff1f9bf4 to your computer and use it in GitHub Desktop.
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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Load plugin cetak nota..\n"); | |
var loader = new PluginLoader(); | |
loader.LoadPlugins(); | |
foreach (var plugin in PluginLoader.Plugins) | |
{ | |
Console.WriteLine("Nama plugin: {0}", plugin.Name); | |
Console.WriteLine("Contoh nota:\n"); | |
plugin.Cetak(); | |
Console.WriteLine("\n----------------------------------------------------------------\n"); | |
} | |
Console.ReadKey(); | |
} | |
} | |
class PluginLoader | |
{ | |
public static List<IPluginCetakNota> Plugins { get; set; } | |
public void LoadPlugins() | |
{ | |
Plugins = new List<IPluginCetakNota>(); | |
var folderPlugins = Path.Combine(Directory.GetCurrentDirectory(), "Plugins"); | |
//Load the DLLs from the Plugins directory | |
if (Directory.Exists(folderPlugins)) | |
{ | |
var files = Directory.GetFiles(folderPlugins); | |
foreach (string file in files) | |
{ | |
if (file.EndsWith(".dll")) | |
{ | |
Assembly.LoadFile(Path.GetFullPath(file)); | |
} | |
} | |
} | |
var interfaceType = typeof(IPluginCetakNota); | |
//Fetch all types that implement the interface IPlugin and are a class | |
var types = AppDomain.CurrentDomain.GetAssemblies() | |
.SelectMany(a => a.GetTypes()) | |
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass) | |
.ToArray(); | |
foreach (var type in types) | |
{ | |
//Create a new instance of all found types | |
Plugins.Add((IPluginCetakNota)Activator.CreateInstance(type)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment