我有以下服务。
[Export(typeof(IPluginsLoaderService))]
internal class PluginsLoaderService : IPluginsLoaderService
{
public PluginsLoaderService()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Shapes"));
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Behaviours"));
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
[ImportMany(typeof (IBehaviorPlugin))]
public IEnumerable<IBehaviorPlugin> BehaviorPlugins { get; set; }
[ImportMany(typeof(IGeometryPlugin))]
public IEnumerable<IGeometryPlugin> ShapePlugins { get; private set; }
}
在其他课程中,我将其导入到这样的属性:
[Import]
public IPluginsLoaderService PluginsLoaderService { get; set; }
PluginsLoaderService不为null,但BehaviorPlugins和ShapePlugins属性包含0个元素。
如果将零件代码组合到下面的函数中,则会加载插件。
[Export(typeof(IPluginsLoaderService))]
internal class PluginsLoaderService : IPluginsLoaderService
{
[ImportMany(typeof (IBehaviorPlugin))]
public IEnumerable<IBehaviorPlugin> BehaviorPlugins { get; set; }
[ImportMany(typeof(IGeometryPlugin))]
public IEnumerable<IGeometryPlugin> ShapePlugins { get; private set; }
public IEnumerable<IBehaviorPlugin> GetBehaviorPlugins()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Shapes"));
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Behaviours"));
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
return BehaviorPlugins;
}
public IEnumerable<IGeometryPlugin> GetShapePlugins()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Shapes"));
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Behaviours"));
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
return ShapePlugins;
}
}
为什么会这样?如何以更好的方式组织它?是否可以在MefBootstrapper内部进行?这是我的引导程序代码。
public class Bootstrapper : MefBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window) this.Shell;
Application.Current.MainWindow.Show();
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));
this.AggregateCatalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Shapes"));
this.AggregateCatalog.Catalogs.Add(new DirectoryCatalog(@"..\..\Plugins\Behaviours"));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ToolbarModule).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ToolboxModule).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(CanvasModule).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(StatusBarModule).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(IPluginsLoaderService).Assembly));
}
}