What is Import Attribute?

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

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.

e.g.

[Import(typeof(ICalculator))]

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.

[Export(typeof(ICalculator))]

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


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