问题描述
我正在努力将脑筋围绕MEF,而我正在努力解决的一件事是 CompositionBatch .
I'm trying to wrap my brain around MEF, and one thing I'm struggling with is CompositionBatch.
CompositionBatch似乎是向MEF系统添加或删除导出的一种方法,但显然它,因此其用途受到限制.由于某种原因,它也可以在容器级别操作,而不是在目录级别操作,其他所有在MEF中注册类型的东西.
CompositionBatch appears to be a way to add or remove exports to the MEF system, but apparently it can't remove something that was added via a catalog, so it's limited in usefulness. It also, for some reason, operates at the container level, instead of at the catalog level with everything else that registers types in MEF.
据我所知,CompositionBatch只是做目录已经可以做的一种冗余方式.如果我想在应用程序启动后注册两种新类型,则不需要CompositionBatch;我也可以将AggregateCatalog传递到我的容器中,然后在我准备好以后,从我要注册的两个新类型中创建一个TypeCatalog,将其添加到AggregateCatalog中,一切将正常进行.但是还有CompositionBatch,它通过向容器中添加方法来解决此目录问题.
As far as I can tell, CompositionBatch is just a redundant way of doing what you could already do with catalogs. If I want to register two new types after my application has started, I don't need CompositionBatch; I could just as well pass an AggregateCatalog to my container, and then later when I'm ready, create a TypeCatalog from the two new types I want to register, add it to the AggregateCatalog, and everything would just work. And yet there's CompositionBatch, which solves this catalog problem by adding methods to the container instead.
我想念什么?为什么存在CompositionBatch?实际上,它对您已经存在的东西做不了的事情吗?为什么我们需要一种在容器级别执行相同操作的方法?
What am I missing? Why does CompositionBatch exist? Does it, in fact, do anything that you can't do with what was already there? Why do we need a way to do the same thing at the container level?
推荐答案
如果目录中有类型,则MEF将在需要时创建该类型的对象. CompositionBatch允许您将对象添加到自己创建的合成中.我们建议总体上坚持使用目录,但是在某些情况下,您需要自己创建对象,或者它是由其他组件(例如XAML解析器)创建的,并且需要将其放入容器中.
If a type is in a catalog, MEF will create an object of that type when needed. A CompositionBatch lets you add objects to the composition that you've created yourself. We recommend sticking to catalogs in general, but there are situations where you need to create an object yourself, or it is created by some other component (for example the XAML parser) and you need to put it in the container.
这篇关于CompositionBatch和目录有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!