What is Import Attribute?

 Posted by Niladri.Biswas on 7/19/2012 | Category: .NET Framework Interview questions | Views: 4482 | Points: 40

MEF is a framework for creating extensible applications that allows developers to discover and use extensions with no configuration required.Part is an object (e.g. a class, a method or a property) that can be imported or exported to the application.In MEF parlance, Import attribute defines the need that a part has.



public ICalculator CalciPlugin { get; set; }

The [Import] attribute declares something to be import.At runtime, the composition engine will fill it when an object is composed. The import must satisfy a contract and the Export has to match the same contract.The interface specified here is
ICalculator.Any export declared with a matching contract will fulfill this import. e.g.


public class Add:ICalculator
#region Interface members
public int GetNumber(int num1, int num2)
return num1 + num2;

Where the interface ICalculator is declared as under

public interface ICalculator

int GetNumber(int num1, int num2);

Multiple values can be imported by using the [ImportMany] attribute.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response