MEF: msdn Article (2). Comparison of Two import methods. Import of attributes or fields vs Constructor

Source: Internet
Author: User

Http://msdn.microsoft.com/zh-cn/magazine/ee291628.aspx

There are two import methods: (1)Import through attributes and fields

public class ViewFactory{  [Import]  public SalesOrderView OrderView { get; set; }}

(2)Import using Constructors

Public class viewfactory
{
[Importingconstructor]
Public viewfactory (salesorderview)
{
}
}

Import through Constructor (usually called constructor injection). When importing constructor,MEF assumes that all parameters are importedSo that you do not need to use the import feature.

It is a matter of personal preferences to import data through constructors rather than attributes,

However, attribute import has a wide range of applicability, especially when attributes exist.When a part is not instantiated by MEF.

The constructor parameter is not supported either.Reassemble.

You can set requiredcreationpolicy for Attribute import, but importingconstructor does not seem to work.

When importing attributes, the constructor does not seem to be able to access the import attributes. This is strange. It seems that the imort attribute can be accessed only after the constructor is executed.

 

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.