Problem: Loading Plugins into a console app(Windows Service Eventually) and running code in the plug in dll's
Research: StructureMap Docs(of course) A few stackoverflow threads this one being the closest. Creating plugin scanner with StructureMap
I have 3 projects: Console App (Driver) 2 Class libraries
Console App
static void Main(string[] args)
{
ObjectFactory.Initialize(cfg => cfg.Scan(scanner =>
{
scanner.AssembliesFromPath(@"PATH TO PLUGIN DIR");
scanner.AddAllTypesOf<IPlugable>();
}));
var list = ObjectFactory.GetAllInstances<IPlugable>();
foreach (var plug in list)
{
plug.Run();
}
}
public interface IPlugable
{
void Run();
}
Plugin_2
public interface IPlugable
{
void Run();
}
public class PlugIn2 : IPlugable
{
public void Run()
{
Console.WriteLine(this.GetType().Name + "fired!");
}
}
public interface IPlugable
{
void Run();
}
public class PlugIn1 : IPlugable
{
public void Run()
{
Console.WriteLine(this.GetType().Name + "fired!");
}
}
The Output:
ObjectFactory.GetAllInstances<IPlugable>();
returns no objects :( Desired Output: 2 Object Instances of Plugin_1 & Plugin_2
Thanks in adv开发者_JS百科ance.
Looks like you are using 3 different interfaces. They are all called "IPlugable", but reside in different namespaces, hence they are not the same.
I think this is the answer I am going with.. Managed Extensibility Framework http://msdn.microsoft.com/en-us/library/dd460648.aspx
Got to love it when you find a new framework to find the exact solution to your problem.
精彩评论