Managed Extensibility Frameowork + Generics


In this article we are going to learn how we could use Managed Extensibility Framework along with Generics .

For basics you could refer my previous articles on MEF . so lets see how we can use Generics in MEF.

Lets create a Generic Class A as shown below :

Notice the attribute Export on top of this class .

    [Export]
   
class A<T> where T : class
    {
    }

I also create a Class B as shown below :
I also decorate the Class B with an Export attribute .

    [Export]
   
class B
    {
    }


Lets now write the Program class .

class Program
    {
        [
Import]
     
public  A<B> a; 

        public virtual void Run()
        {
           
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
           
var container = new CompositionContainer(catalog);

            container.ComposeParts(this);

            Console.WriteLine(a.GetType());
         
//  Logger.Write("Helloworld");
        
        }     

        static void Main(string[] args)
        {
           
Program p = new Program();
            p.Run();
 
              
Console.ReadLine();
          
        }
    }


I create an object of Type A<B> . Notice the attribute Import on top of the object .

Well we are done now . Thanks .
 

Ebook Download
View all
Learn
View all