79 lines
2.9 KiB
C#
79 lines
2.9 KiB
C#
// See https://aka.ms/new-console-template for more information
|
|
using System.Reflection;
|
|
|
|
|
|
namespace dopt.TOM.Integration
|
|
{
|
|
internal class Program
|
|
{
|
|
static internal bool atRuntime = true;
|
|
static void Main(string[] args)
|
|
{
|
|
string id = "345";
|
|
string filename = "Dummy_Dataset_N_1000.csv";
|
|
|
|
int idx = 0;
|
|
int? idxID, idxDyn;
|
|
idxID = null; idxDyn = null;
|
|
foreach (string arg in args)
|
|
{
|
|
// ID
|
|
if (arg == "-id") idxID = idx + 1;
|
|
if (idxID != null && idxID == idx) id = arg;
|
|
// dynamic loading
|
|
if (arg == "-dyn") idxDyn = idx + 1;
|
|
if (idxDyn != null && idxDyn == idx) atRuntime = Convert.ToBoolean(arg);
|
|
|
|
idx++;
|
|
}
|
|
|
|
if (atRuntime) dynamicBind(id, filename);
|
|
else directBind(id, filename);
|
|
Console.WriteLine("-----------------------------------------------------");
|
|
}
|
|
|
|
static void directBind(string id, string filename)
|
|
{
|
|
Console.WriteLine("Test for static binding at buildtime...");
|
|
var plugin = new Plugin("");
|
|
Console.WriteLine("First invocation...");
|
|
plugin.RunOnCSV(id, filename);
|
|
//Console.WriteLine("Second invocation...");
|
|
//plugin.RunOnCSV(id, filename);
|
|
}
|
|
static void dynamicBind(string id, string filename)
|
|
{
|
|
Console.WriteLine("Test for dynamic loading at runtime...");
|
|
// correct DLL path
|
|
string relPathToDll = @".\dopt.TOM.dll";
|
|
Type currentType = typeof(Program);
|
|
Assembly assemblySelf = currentType.Assembly;
|
|
Console.WriteLine($"Location:\n{assemblySelf.Location}");
|
|
string assemPath = assemblySelf.Location;
|
|
if (assemPath.StartsWith("file:///"))
|
|
{
|
|
assemPath = assemPath.Replace("file:///", "");
|
|
}
|
|
string exeDir = System.IO.Path.GetDirectoryName(assemPath);
|
|
string pathToDll = Path.Combine(exeDir, relPathToDll);
|
|
// load type and execute method
|
|
Assembly pluginDll = Assembly.LoadFrom(pathToDll);
|
|
Type Plugin = pluginDll.GetType("dopt.TOM.Plugin");
|
|
object instance = Activator.CreateInstance(Plugin, "");
|
|
MethodInfo[] methods = Plugin.GetMethods();
|
|
|
|
Console.WriteLine("Available methods are:");
|
|
foreach (MethodInfo method in methods)
|
|
{
|
|
Console.WriteLine(method.Name);
|
|
}
|
|
|
|
Console.WriteLine($"Trying to invoke: {methods[0]}");
|
|
object[] parameters = { id, filename };
|
|
Console.WriteLine("First invocation...");
|
|
methods[0].Invoke(instance, parameters);
|
|
//Console.WriteLine("Second invocation...");
|
|
//methods[0].Invoke(instance, parameters);
|
|
}
|
|
}
|
|
} |