I am trying to load types from a DLL that all implement an interface. I want to be able to get the types from the DLL, create and instance of each type and then treat each type as if it implements the interface. I have written a basic example below of what I'm trying to achieve.
Thanks for your help
class Program
{
private static List<IJob> _jobs;
static void Main(string[] args)
{
LoadJobs();
foreach (var job in _jobs)
{
job.Run();
}
Console.ReadKey();
}
static void LoadJobs()
{
_jobs = new List<IJob>();
var jobsPath = Directory.GetFiles(@"C:\Jobs\");
foreach (var file in jobsPath)
{
var assembly = Assembly.LoadFrom(file);
foreach (var type in assembly.GetExportedTypes())
{
var instance = Activator.CreateInstance(type);
var job = instance as IJob;
if(job != null)
_jobs.Add(job);
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire