开发者

Getting exported part instance from MEF container

How I can the existing instance of an exported part in MEF container . If I have class A which was composed in the container , I need in some places in my code to get the instan开发者_开发问答ce , if I call GetExortedValue() , then if class A signed with CreationPolicy.NonShared , then it'll be instantiated again and I need the current one .

Thanks in advance ...


Obviously calling GetExportedValue<T> on your container could result on the generation of a new instance of T (depending on the CreationPolicy used for the part), but there is an option to call GetExport<T> which will return you a Lazy<T> instance. This is the singular part that is generated and only generated the once:

var part = container.GetExport<IMyInterface>();

In the above example, part would be an instance of Lazy<IMyInterface>, so when you first access part.Value, the delegate bound in the Lazy<IMyInterface> calls back to the container to create and compose the IMyInterface instance and is returned. Subsequent calls to part.Value will always return this same instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜