First Encounters of the MEF Kind

added by mceranski
3/15/2010 5:39:49 AM


One of the newest additions to the .NET 4 framework is MEF. MEF stands for the Managed Extensibility Framework and is the answer to your prayers if you are building an app that allows developers to extend its functionality by building plugins. MEF makes the task of loading assemblies and utilizing their functionality very simple. Before we go any further there are a few basic terms that you must understand before you start using MEF: The Catalog is responsible for loading assemblies. There are several types of catalogs to chose from such as DirectoryCatalog and AssemblyCatalog which we will discuss in more detail later on. The Container holds one or more catalogs. The Export attribute is used to decorate objects so they can be consumed by MEF. The Import attribute lets MEF know that an object is the target for one of more exported objects. A part is any object that is exported or imported. So in order to start using MEF, you need to create one or more catalogs, add them to a container and then call a method named “ComposeParts” which will search the assemblies in the catalog for “parts” which are marked with export or import attributes. In order to help understand the process better, lets look at some code..