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