I. Overview of the Presentation
This presentation introduces the life cycle management of MEF, with emphasis on the three creation strategies for exporting parts: Creationpolicy.any, creationpolicy.shared, creationpolicy.nonshared.
related Download (screen recorder): http://yunpan.cn/cjZEXHY8N3D6z access Password 3912
Warm tip: If the screen recording and code can not be downloaded, you can leave a message in the station, or send an email to [email protected]
Welcome to be interested in research. NET related technology users add QQ Group: 18507443
Ii. Introduction to the part creation strategy
The three creation strategies for the exported parts provided by MEF are as follows:
Creationpolicy.any: Indicates shareable or unshared, an instance of a part is automatically controlled by a MEF container according to different request requirements.
Creationpolicy.shared represents a shared part, a plug-in part of a shared type can be used in multiple MEF combo containers, and multiple requests for that part to return are actually the same object.
The creationpolicy.nonshared type, which means that the part instance is not shared, and a new object instance is created whenever a new request is made.
Three, the key code
Here's how to set up its creation policy in the export part, and how to use it when importing.
By adding the Partcreationpolicyattribute attribute to the exported part, you can specify its creation policy. The code is as follows:
[CSharp]View Plaincopy
- [Partcreationpolicy (creationpolicy.nonshared)]
- [Export (typeof (UserControl))]
- Public partial class Usercontrolbb:usercontrol
- {
- Public USERCONTROLBB ()
- {
- InitializeComponent ();
- }
- }
When exporting a part, you can set the import to specify the part that creates the policy, such as the following code, importing only the exported parts that create the policy as creationpolicy.shared.
[CSharp]View Plaincopy
- [ImportMany (Requiredcreationpolicy = creationpolicy.shared)]
- Public list<usercontrol> UserControls { get; set;}
Iv. Related Resources
1. MSDN Official information: http://msdn.microsoft.com/zh-cn/library/dd460648 (v=vs.110). aspx
Life cycle of MEF components (Partcreationpolicy)