What is Export Attribute in MEF?

 Posted by Niladri.Biswas on 7/19/2012 | Category: .NET Framework Interview questions | Views: 4878 | 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.The parts are discovered dynamically via catalogs.In MEF parlance, Import attribute defines the need that a part has.The Export attribute fulfills that. It exposes those parts that will participate in the composition.

e.g. say we have an interface as under

public interface ICalculator

{
int GetNumber(int num1, int num2);
}

any object that wants to be a part (Export component) of MEF composition has to satisfy this Interface

e.g.

[Export(typeof(ICalculator))]

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


is a valid Export component but not the below

[Export(typeof(ICalculator))]

public class Add:INonMEFCalculator
{
#region Interface members
public void SayHello()
{
//do something
}
#endregion
}


because it does not satisfy the need that a part need in this scenario.

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


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response