指示MEF使用任何可用的组件(Instruct MEF to use any available assemblies)

编程入门 行业动态 更新时间:2024-10-13 18:25:31
指示MEF使用任何可用的组件(Instruct MEF to use any available assemblies)

我正在使用.NET 4.0中的System.ComponentModel.Composition在Visual Studio 2010 beta 2中首次尝试使用Managed Extensibility Framework。

我一直无法让CompositionContainer使用下面的两个替代例程来查找我的实现程序集。

第一次尝试(这在MEF的旧版codeplex发行版中有效):

var composition = new CompositionBatch(); composition.AddPart(this); var container = new CompositionContainer(new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory)); container.Compose(composition);

第二次尝试(我认为这在第一次测试中起作用):

var aggregateCatalog = new AggregateCatalog( new AssemblyCatalog(Assembly.GetExecutingAssembly()), new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory)); var compositionContainer = new CompositionContainer(aggregateCatalog); compositionContainer.ComposeParts(this);

测试版2有没有新的方法?

编辑:它变成与组成无关。 我有一个静态属性表示我的导入实现:

[Import] public static ILog Log { get; set; }

应该是:

[Import] public ILog Log { get; set; }

我将丹尼尔的回答标记为接受,因为以更彻底的方式进行调试的忠告解决了问题。

I am trying out the Managed Extensibility Framework for the first time in Visual Studio 2010 beta 2 using the System.ComponentModel.Composition from .net-4.0.

I have been unable to get the CompositionContainer to find my implementation assemblies using the two alternative routines below.

First attempt (this worked in an older codeplex release of MEF):

var composition = new CompositionBatch(); composition.AddPart(this); var container = new CompositionContainer(new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory)); container.Compose(composition);

Second attempt (this worked in beta 1, I think):

var aggregateCatalog = new AggregateCatalog( new AssemblyCatalog(Assembly.GetExecutingAssembly()), new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory)); var compositionContainer = new CompositionContainer(aggregateCatalog); compositionContainer.ComposeParts(this);

Is there a new way to do this in beta 2?

EDIT: It turned out to be nothing to do with the composition. I had a static property representing my imported implementation:

[Import] public static ILog Log { get; set; }

which should have been:

[Import] public ILog Log { get; set; }

I marked Daniel's answer as accepted because the sage advice of debugging in a more thorough fashion solved the problem.

最满意答案

什么是失败? 有没有一个你期望得到满足的进口不满意? 你是否调用GetExport()并且失败?

创建目录后,您可以在调试器中中断,然后将鼠标悬停在aggregateCatalog变量上以检查它并查看其中的部件。 我的猜测是部件可能在目录中,并且问题在代码中的其他位置。 一个可能的原因是,您有一个使用[Import]属性而不是[ImportMany]的集合导入,和/或您的零件由于导入不能满足而被拒绝。

What is failing? Is there an import you expect to be satisfied which is not being satisfied? Are you calling GetExports() and it is failing?

You can break in the debugger after the catalog has been created, and mouse over the aggregateCatalog variable to inspect it and see what parts are in it. My guess is that the parts are probably in the catalog, and the problem is somewhere else in your code. A likely cause is that you have a collection import which is using the [Import] attribute instead of [ImportMany], and/or that your parts are being rejected because they have imports that can't be satisfied.

更多推荐

本文发布于:2023-08-07 17:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465364.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   指示   assemblies   MEF   Instruct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!