使用 MEF 对 ImportMany 进行重组时出现问题
我在一个类上定义了一个属性,该类定义了 importManyAttribute ,声明如下:
public const string FontStyleProvidersPropertyName = "FontStyleProviders";
[ImportMany(typeof(IFontStyleProvider), RequiredCreationPolicy = CreationPolicy.Shared, AllowRecomposition=true)]
public List<IFontStyleProvider> FontStyleProviders { get; set; }
第一次运行时,我构建了我的组合容器,如下所示
private CompositionContainer BuildCompositionContainer()
{
//build our composable parts catalog
Assembly executingAssembly = Assembly.GetExecutingAssembly();
CompositionContainer applicationContainer;
string localPath = Path.GetDirectoryName(executingAssembly.Location);
try
{
aggregateCatalog = new AggregateCatalog();
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(executingAssembly));
if (!Directory.Exists(Path.Combine(localPath, ApplicationExtensionsPath)))
{
Directory.CreateDirectory(Path.Combine(localPath, ApplicationExtensionsPath));
}
exportsCatalog = new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath));
aggregateCatalog.Catalogs.Add(new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath)));
//create a composition container
return applicationContainer = new CompositionContainer(aggregateCatalog);
}
catch (Exception e)
{
Debug.Fail("Catalog Construction Failed", e.StackTrace);
throw;
}
}
此时,一切都按预期工作,但我似乎无法触发“this”类实例的重新组合。我有一个导入方法如下:
private void Import()
{
exportsCatalog.Refresh();
CompositionBatch batch = new CompositionBatch();
batch.AddPart(this);
applicationContainer.Compose(batch);
var copy = PropertyChanged;
if (copy != null)
{
copy(this, new PropertyChangedEventArgs(FontStyleProvidersPropertyName));
copy(this, new PropertyChangedEventArgs(MessageContainerViewModelsPropertyName));
}
}
它在exportsCatalog使用的ApplicationExtensionPath文件夹中找到新类型就好了,但它实际上从未重新构建FontStyleProviders(或MessageContainerViewModels)
我已经浏览了几次文档,但我不能似乎明白为什么。
I've got a property defined on a class that has the importManyAttribute defined for it, the declaration is as follows:
public const string FontStyleProvidersPropertyName = "FontStyleProviders";
[ImportMany(typeof(IFontStyleProvider), RequiredCreationPolicy = CreationPolicy.Shared, AllowRecomposition=true)]
public List<IFontStyleProvider> FontStyleProviders { get; set; }
on first run I build my composition container as follows
private CompositionContainer BuildCompositionContainer()
{
//build our composable parts catalog
Assembly executingAssembly = Assembly.GetExecutingAssembly();
CompositionContainer applicationContainer;
string localPath = Path.GetDirectoryName(executingAssembly.Location);
try
{
aggregateCatalog = new AggregateCatalog();
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(executingAssembly));
if (!Directory.Exists(Path.Combine(localPath, ApplicationExtensionsPath)))
{
Directory.CreateDirectory(Path.Combine(localPath, ApplicationExtensionsPath));
}
exportsCatalog = new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath));
aggregateCatalog.Catalogs.Add(new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath)));
//create a composition container
return applicationContainer = new CompositionContainer(aggregateCatalog);
}
catch (Exception e)
{
Debug.Fail("Catalog Construction Failed", e.StackTrace);
throw;
}
}
At this point, everything works as expected, but I can't seem to trigger re-composition on "this" class instance. I have an import method as follows:
private void Import()
{
exportsCatalog.Refresh();
CompositionBatch batch = new CompositionBatch();
batch.AddPart(this);
applicationContainer.Compose(batch);
var copy = PropertyChanged;
if (copy != null)
{
copy(this, new PropertyChangedEventArgs(FontStyleProvidersPropertyName));
copy(this, new PropertyChangedEventArgs(MessageContainerViewModelsPropertyName));
}
}
It finds new types in the ApplicationExtensionPath folder used by the exportsCatalog just fine, but it never actually re-builds FontStyleProviders (or MessageContainerViewModels)
I've been through the documents a few times and I can't seem to figure out why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您实际上并未将调用
Refresh()
的目录添加到 AggregateCatalog 中。更改为:改为:
另外,一旦您的课程已撰写完毕,您就不应该再次撰写。只需调用
exportsCatalog.Refresh()
就足以导致重组。The problem is you aren't actually adding the catalog which you call
Refresh()
on to the AggregateCatalog. Change this:To this:
Also, once your class has been composed once you shouldn't compose it again. Just calling
exportsCatalog.Refresh()
will be enough to cause recomposition.