Skip to content

Instantly share code, notes, and snippets.

@k4m4r82
Created September 19, 2020 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k4m4r82/d6b0bae79d491ff87e363482ff1f9bf4 to your computer and use it in GitHub Desktop.
Save k4m4r82/d6b0bae79d491ff87e363482ff1f9bf4 to your computer and use it in GitHub Desktop.
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