C# MEF cannot do type binding?

Feb 24, 2011 at 8:47 PM
I would like to do something like :

    Bind<Weapon>().To<Sword>();

Each time I request a new Weapon from my Samurai object that I get a Sword.

I try to do it with MEF but I need every time to inject stuff once the object is instanced... that's not what I want.

Example:

 

var catalog = new AssemblyCatalog(typeof(...).Assembly);

var container = new CompositionContainer(catalog); var batch = new CompositionBatch();
var mySamurai = new Samurai();
batch.AddPart(mySamurai);//I would prefer the type not an object... container.Compose(batch);

mySamurai .Attack();

 
That's works BUT I would like to do something like:


var catalog = new AssemblyCatalog(typeof(...).Assembly);

var container = new CompositionContainer(catalog);

var batch = new CompositionBatch();

batch.AddPart(typeof(Samurai));//HERE container.Compose(batch);       
var mySamurai = new Samurai();

mySamurai .Attack();