MEF:目录部分目录

发布于 2024-09-11 06:08:53 字数 488 浏览 2 评论 0原文

我正在尝试编写一个简单的 MEF 演示来了解它。我正在关注 这个教程,但似乎已经过时了。可下载的示例可以工作,但它使用的包含程序集比 Framwework 4 附带的当前版本 (4.0) 早 2 个版本 (2008.9.4.0)。

特别是,它使用了 DirectoryPartCatalog,我在最新库中找不到它。谁能提供一个关于如何从当前版本的 MEF 目录中发现可插入程序集的示例?

谢谢

I'm trying to write a simple MEF demo to learn about it. I'm following this tutorial, but it seems to be outdated. The downloadable example works, but it uses an included assembly that is 2 versions older (2008.9.4.0) than the current one (4.0) that ships with Framwework 4.

In particular, it uses DirectoryPartCatalog that I cannot find anywhere in the newest library. Could anyone provide an example on how to discover pluggable assemblies from a directory with the current version of MEF?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

樱&纷飞 2024-09-18 06:08:53

您需要进行一些更改才能使该示例使用 System.ComponentModel.Composition 的内置版本进行编译和运行。

class Program
{
    [ImportMany] // [Import]
    public IEnumerable<string> Messages { get; set; }

    [ImportMany] // [Import]
    public IEnumerable<IOutputString> OutputSet { get; set; }

    [Import("OutputMessages")]
    public Action<IEnumerable<IOutputString>, IEnumerable<string>> OutputMessages { get; set; }

    public void Run()
    {

        var catalog = new AggregateCatalog(); // AggregatingComposablePartCatalog
        catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\..\ExternalMessages\bin\Debug")); // DirectoryPartCatalog
        catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\..\ExtraMessages")); // DirectoryPartCatalog
        catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); // AttributedAssemblyPartCatalog
        var container = new CompositionContainer(catalog); // CompositionContainer(catalog.CreateResolver());

        // container.AddPart(this);
        // container.Compose();
        container.ComposeParts(this);

        OutputMessages(OutputSet, Messages);

    }

    static void Main(string[] args)
    {
        Program p = new Program();
        p.Run();
    }
}

You need to make several changes to make this sample compile and run with builtin version of System.ComponentModel.Composition.

class Program
{
    [ImportMany] // [Import]
    public IEnumerable<string> Messages { get; set; }

    [ImportMany] // [Import]
    public IEnumerable<IOutputString> OutputSet { get; set; }

    [Import("OutputMessages")]
    public Action<IEnumerable<IOutputString>, IEnumerable<string>> OutputMessages { get; set; }

    public void Run()
    {

        var catalog = new AggregateCatalog(); // AggregatingComposablePartCatalog
        catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\..\ExternalMessages\bin\Debug")); // DirectoryPartCatalog
        catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\..\ExtraMessages")); // DirectoryPartCatalog
        catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); // AttributedAssemblyPartCatalog
        var container = new CompositionContainer(catalog); // CompositionContainer(catalog.CreateResolver());

        // container.AddPart(this);
        // container.Compose();
        container.ComposeParts(this);

        OutputMessages(OutputSet, Messages);

    }

    static void Main(string[] args)
    {
        Program p = new Program();
        p.Run();
    }
}
清风挽心 2024-09-18 06:08:53

DirectoryPartCatalog 现在称为 DirectoryCatalog

DirectoryPartCatalog is now called DirectoryCatalog

酒废 2024-09-18 06:08:53

我认为您正在搜索 DirectoryCatalog< /a>

I think, that you are searching for DirectoryCatalog

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