Moved github https://github.com/powerumc/MefGeneric

Junil, Um (엄준일)
Microsoft Team System MVP Since ~2012
Blog : http://blog.powerumc.kr

Introduction

MEFGeneric is a framework to support CLR Generic types in MEF (Managed Extensibility Framework).

Background

MEF does not support open generic types. Therefore, open generic type classes cannot utilize MEF Composition. But, via MEFGeneric, changing and/or extending current MEF source code allows use of open generic types in MEF.

Example

The following source code does not work in MEF, but using MEFGeneric, you can resolve and compose objects.

Catalog and Composition
var catalog = new AggregateCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
var genericCatalog = new GenericCatalog(catalog);
var container = new CompositionContainer(genericCatalog);

Export Definition
public interface IUMC<T>
{
	void Say();
}

// Important : via only MEFGeneric, you can export definition of IUMC<> generic types.
[Export(typeof(IUMC<>))]
public class UMC<T> : IUMC<T>
{
	#region IUMC<T> Member

	public void Say()
	{
		Console.WriteLine(typeof(T).FullName);
	}

	#endregion
}

Container Resolving / Injection
container.GetExportedValueOrDefault<IUMC<int>>().Say();
container.GetExportedValueOrDefault<IUMC<string>>().Say();

Last edited Jun 21, 2013 at 5:19 AM by umjunil, version 24