使用 MEF 对 ImportMany 进行重组时出现问题

发布于 2024-09-14 20:11:24 字数 2135 浏览 0 评论 0原文

我在一个类上定义了一个属性,该类定义了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2024-09-21 20:11:24

问题是您实际上并未将调用 Refresh() 的目录添加到 AggregateCatalog 中。更改为:

exportsCatalog = new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath));
aggregateCatalog.Catalogs.Add(new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath)));

改为:

exportsCatalog = new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath));
aggregateCatalog.Catalogs.Add(exportsCatalog);

另外,一旦您的课程已撰写完毕,您就不应该再次撰写。只需调用 exportsCatalog.Refresh() 就足以导致重组。

The problem is you aren't actually adding the catalog which you call Refresh() on to the AggregateCatalog. Change this:

exportsCatalog = new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath));
aggregateCatalog.Catalogs.Add(new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath)));

To this:

exportsCatalog = new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath));
aggregateCatalog.Catalogs.Add(exportsCatalog);

Also, once your class has been composed once you shouldn't compose it again. Just calling exportsCatalog.Refresh() will be enough to cause recomposition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文