try
{
// do my job or throw exception
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
var assembly = Assembly.LoadFrom("c:\plugins\table.dll");
var type = assembly.GetType("MyNamespace.Table");
var instance = (ITable) Activator.CreateInstance(type);
namespace Common
{
public interface IPlugin
{
string GetName();
}
}
using Common;
namespace Plugin
{
public sealed class Plugin : IPlugin
{
public string GetName()
{
return "Cool Plugin!";
}
}
}
using Common;
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
namespace PluginsTest
{
class Program
{
static void Main(string[] args)
{
var plugin = LoadPlugin("..\\..\\..\\Plugin\\bin\\Debug\\Plugin.dll");
var name = plugin.GetName();
Debug.WriteLine("Plugin loaded: " + name);
}
static IPlugin LoadPlugin(string path)
{
var type = Assembly
.LoadFrom(path)
.GetTypes()
.First(typeof(IPlugin).IsAssignableFrom);
return (IPlugin)Activator.CreateInstance(type);
}
}
}
var sw = new Stopwatch();
sw.Start();
await MySuperCoolJobAsync();
sw.Stop();
Trace.WriteLine($"{sw.ElapsedMilliseconds} мс");