Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.