C# MEF 不能进行类型绑定?

发布于 2024-10-19 02:38:59 字数 712 浏览 2 评论 0原文

我想按类型绑定而不是绑定到实例对象。

我现在要做的:

var catalog = new AssemblyCatalog(typeof(...).Assembly);

var container = new CompositionContainer(catalog); 
    var batch = new CompositionBatch(); 
var mySamurai = new Samurai(); 
batch.AddPart(mySamurai);//I would prefer the type not an object...    
    container.Compose(batch);
mySamurai.Attack();

这是可行的,但我想做一些类似的事情:

var catalog = new AssemblyCatalog(typeof(...).Assembly);
var container = new CompositionContainer(catalog);
var batch = new CompositionBatch();

batch.AddPart(typeof(Samurai));//HERE container.Compose(batch);        
var mySamurai = new Samurai();
mySamurai.Attack(); 

MEF 可以吗?

I would like to bind by type instead to the object instanced.

What I have to do NOW :

var catalog = new AssemblyCatalog(typeof(...).Assembly);

var container = new CompositionContainer(catalog); 
    var batch = new CompositionBatch(); 
var mySamurai = new Samurai(); 
batch.AddPart(mySamurai);//I would prefer the type not an object...    
    container.Compose(batch);
mySamurai.Attack();

That's works BUT I would like to do something like:

var catalog = new AssemblyCatalog(typeof(...).Assembly);
var container = new CompositionContainer(catalog);
var batch = new CompositionBatch();

batch.AddPart(typeof(Samurai));//HERE container.Compose(batch);        
var mySamurai = new Samurai();
mySamurai.Attack(); 

Is that possible with MEF?

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

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

发布评论

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

评论(3

泪冰清 2024-10-26 02:38:59

通常,您可以使用属性在 MEF 中设置导出和导入,而不是像 Ninject 那样在代码中配置它们。

即使 MEF 没有开箱即用地进行“代码配置”,您仍然可以使用 MEFContrib 项目使用工厂导出提供程序来完成此操作。

更新:在MEF2-Preview3中属性-添加了更少的注册

另外,Mark Seemann 发表了博客,介绍了一种即使没有任何类型也可以“注册”类型的方法通过巧妙地利用属性导出和泛型,使用新的无属性注册。

Normally you set up exports and imports in MEF with attributes, instead of configuring them in code like Ninject does.

Even though MEF does not do "configuration in code" out of the box, you can still use the MEFContrib project to do that with the factory export provider.

update: in MEF2-Preview3 attribute-less registration was added.

Also, Mark Seemann blogged about a way to "register" types even without using the new attribute-less registration, by making clever use of property exports and generics.

尤怨 2024-10-26 02:38:59

如果我理解正确的话,没有办法做到这一点。 MEF 在看到 fakeEntity2 时无法执行任何操作,因为 MEF 从未真正“看到”它。您必须像处理 fakeEntity 一样将其传递到容器,或者必须导出 FakeEntity 类,并以某种方式将其从容器中拉出(即使用 GetExportedValue)。

If I'm understanding you correctly, there's no way to do this. MEF can't do anything when it sees fakeEntity2 because MEF never actually "sees" it. You have to pass it to the container as you do with fakeEntity, or you have to export the FakeEntity class, and pull it from the container somehow (ie with GetExportedValue).

别把无礼当个性 2024-10-26 02:38:59

我对 MEF 不太了解,但你的情况看起来像是 Ninject、Unity、StuctureMap、Castle Windsor 等 IoC 容器所擅长的。

I don't know much about MEF, but your situation looks like something an IoC Container like Ninject, Unity, StuctureMap, Castle Windsor, et al... would excel at.

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