MEF:ComposeParts缺失

Mat*_*att 20 .net c# mef

我正在尝试遵循一些在.Net 4中使用MEF的入门指南,但是当我开始设置应用程序时,我会遇到困难.说明说这样做:

var catalog = new DirectoryCatalog(@".\");
var container = new CompositionContainer(catalog);
container.Composeparts(this);
Run Code Online (Sandbox Code Playgroud)

但是.Net 4中包含的System.ComponentModel.Composition版本似乎没有在CompositionContainer上使用Composeparts方法,我无法在当前系统中找到有关如何执行此操作的良好参考.

以下是我目前使用的参考:使用 Managed Extensibility Framework在.NET 4中构建可组合应用程序

有没有人有更好的参考,我应该看看?

And*_*ndo 26

CompositionContainer确实有一个ComposeParts方法,作为扩展方法.

有关一些工作代码,请参阅参考.

  • 为什么哦为什么哦为什么它是一个扩展方法!谢谢你的回答 :-) (3认同)

小智 22

有一点需要注意,如果您之前没有使用过扩展方法.你必须有using语句.在这种情况下:

using System.ComponentModel.Composition;
Run Code Online (Sandbox Code Playgroud)

使问题中的代码起作用.如果没有using语句,intellisense和编译器将不允许使用扩展方法.