MEF: difference between GetExportedValue and SatisfyImports

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 15:41:24

As far as I know (from experimenting and looking at MEF's source code):

  1. When a container is disposed, all exported catalog parts that are disposable are also disposed. The exported catalog parts are the ones either decorated with the ExportAttribute or specified as exports using the RegistrationBuilder (MEF2). These parts are created by the container and their lifetime is dependent on the lifetime of the container itself.
  2. On the other hand, objects manually created and composed using either CompositionContainer.SatisfyImportsOnce or CompositionContainer.ComposeParts will not be disposed. Their lifetime is not dependent on the lifetime of the container.

Now the difference between GetExportedValue and SatisfyImports is not the same. GetExportedValue returns all exported parts that are registered with the container. This includes the parts created by the container (the exported catalog parts mentioned in 1.) plus the parts registered using CompositionContainer.ComposeParts. SatisfyImports will inject any imports that are available but will not register the object as an export even if its class is marked as an exported type (see 1.). Additionally, SatisfyImports will disable recomposition, but this is off topic.

MEF's documentation on CodePlex provides valuable info on Parts Lifetime.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!