C # MEF can't do type binding?

I would like to bind by type to an instanced object.

What I need to do now:

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();

This 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(); 

Is this possible with MEF?

+3
source share
3 answers

Usually you configure export and import in MEF with attributes, but do not configure them in code, for example, Ninject.

Although MEF does not “configure in code” out of the box, you can still use the MEFContrib project to do this with the factory export provider .

update : in MEF2-Preview3 registration without attributes .

, Mark Seemann blogged "" , , .

+1

, . MEF , fakeEntity2, MEF "" . , fakeEntity, FakeEntity - ( GetExportedValue).

+1

I don’t know much about MEF, but your situation looks like something like an IoC container like Ninject, Unity, StuctureMap, Castle Windsor etc ... will be superior.

0
source

Source: https://habr.com/ru/post/1794517/


All Articles