MEF Composition.NET4.0

MEF Composition.NET4.0,mef,Mef,提前感谢您的帮助。我有以下导出部件: [Export (typeof(INewComponent))] // orignally tried just [Export} here and importing NewComponent below public class NewComponent : INewComponent { // does stuff including an import } 控制台测试程序导入上述内容: public class Program

提前感谢您的帮助。我有以下导出部件:

[Export (typeof(INewComponent))]  // orignally tried just [Export} here and importing NewComponent below
public class NewComponent : INewComponent  
{  
    // does stuff including an import  
}
控制台测试程序导入上述内容:

public class Program   
{    

    [Import]  // have tried variations on importing "NewComponent NewComponent" etc  
    public INewComponent NewComponent
    {
        get;
        set;
    }

    public static void Main(string[] args)
    {
        var p = new Program();
        var catalog = new AssemblyCatalog(typeof(Program).Assembly);
        var container = new CompositionContainer(catalog);
        container.ComposeParts(p);
}
由于这些CompositionException,合成失败(我删除了名称空间以保护罪犯:):

1) 未找到与约束匹配的有效导出 “((exportDefinition.ContractName==“INewComponent”)和 (exportDefinition.Metadata.ContainsKey(“ExportTypeIdentity”)和 “INewComponent”.Equals(exportDefinition.Metadata.get_项(“ExportTypeIdentity”))”, 无效的导出可能已被拒绝

如果我在主程序中按以下方式进行构图,则构图会成功:

public class Program  
{      

    public static void Main(string[] args)
    {
        INewComponent newComponent = new NewComponent();

        var catalog = new AssemblyCatalog(typeof(Program).Assembly);
        var container = new CompositionContainer(catalog);
        container.ComposeParts(newComponent);
    }
}

谢谢

您的导出零件是否包含在与
程序
相同的组件中?如果它位于单独的DLL中,则还需要将该程序集包括在目录中,如下所示:

var aggregateCatalog = new AggregateCatalog();
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(NewComponent).Assembly));
var container = new CompositionContainer(aggregateCatalog);
// etc...
如果这不起作用,那么有一个很好的开源工具叫做VisualMefx,可以帮助您分析目录。下面是一篇关于设置它的短文:


您的导出零件是否与
程序包含在同一个部件中?如果它位于单独的DLL中,则还需要将该程序集包括在目录中,如下所示:

var aggregateCatalog = new AggregateCatalog();
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(NewComponent).Assembly));
var container = new CompositionContainer(aggregateCatalog);
// etc...
如果这不起作用,那么有一个很好的开源工具叫做VisualMefx,可以帮助您分析目录。下面是一篇关于设置它的短文:


在您的
NewComponent
课程中,您编写了以下内容:

// does stuff including an import
如果未关闭的导入有问题,MEF将投诉
程序.NewComponent
导入,而不是实际的深层原因。这就是所谓的“稳定成分”,但是

您可以按照MEF文档中的说明了解实际原因


在一个小程序中,您还可以尝试调用
container.GetExportedValue()
进行一些导出,直到找到导致问题的导出。

在您的
新组件
类中,您编写了以下内容:

// does stuff including an import
如果未关闭的导入有问题,MEF将投诉
程序.NewComponent
导入,而不是实际的深层原因。这就是所谓的“稳定成分”,但是

您可以按照MEF文档中的说明了解实际原因


在一个小程序中,您还可以尝试调用
container.GetExportedValue()
进行一些导出,直到找到导致问题的导出。

谢谢Jim。是的,NewcomComponent是在一个DLL中,上面的方法就成功了。我需要回去重新阅读Block关于MEF的原始MSDN文章:)。h2上博客的Thx使用Visual MEFX。太好了,谢谢你,吉姆。是的,NewcomComponent是在一个DLL中,上面的方法就成功了。我需要回去重新阅读Block关于MEF的原始MSDN文章:)。h2上博客的Thx使用Visual MEFX。太好了,谢谢你,维姆。是的,我熟悉稳定组合项:}。文章中关于SKU配置的想法非常有趣。谢谢Wim。是的,我熟悉稳定组合项:}。文章中关于SKU配置的想法非常有趣。