首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MEF: DirectoryPartCatalog

MEF: DirectoryPartCatalog
EN

Stack Overflow用户
提问于 2010-07-23 16:02:18
回答 3查看 734关注 0票数 6

我正在试着写一个简单的MEF演示来学习它。我正在学习this教程,但它似乎已经过时了。可下载的示例可以工作,但它使用的包含的程序集比Framwework4附带的当前版本(4.0)旧了2个版本(2008.9.4.0)。

特别是,它使用了我在最新的库中找不到的DirectoryPartCatalog。有没有人能举个例子,说明如何用当前版本的MEF从目录中发现可插拔的程序集?

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-07-23 17:41:19

您需要进行几处更改,才能使此示例在内置版本的System.ComponentModel.Composition中编译和运行。

代码语言:javascript
复制
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();
    }
}
票数 6
EN

Stack Overflow用户

发布于 2010-07-23 17:33:56

DirectoryPartCatalog现在称为DirectoryCatalog

票数 2
EN

Stack Overflow用户

发布于 2010-07-23 17:35:13

我想,你是在找DirectoryCatalog

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3316347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档