Life cycle of MEF components (Partcreationpolicy)

Source: Internet
Author: User

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
    1. [Partcreationpolicy (creationpolicy.nonshared)]
    2. [Export (typeof (UserControl))]
    3. Public partial class Usercontrolbb:usercontrol
    4. {
    5. Public USERCONTROLBB ()
    6. {
    7. InitializeComponent ();
    8. }
    9. }

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
    1. [ImportMany (Requiredcreationpolicy = creationpolicy.shared)]
    2. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.