MEF查询组合容器

查询组合容器
当查询一个简单实例时返回null或者一个以上时会出异常。

GetExportedValue();

var container = new CompositionContainer(new AssemblyCatalog(typeof(Program).Assembly));
Root partInstance = container.GetExportedValue<Root>();


[Export("my_contract_name")]
public class Root
{
}

var container = new CompositionContainer(new AssemblyCatalog(typeof(Program).Assembly));
Root partInstance = container.GetExportedValue<Root>("my_contract_name");


GetExport();
Lazy<Root> export = container.GetExport<Root>();
var root = export.Value; //create the instance.

GetExportedValueOrDefault();
var root = container.GetExportedValueOrDefault<Root>(); // may return null

 

 

posted @ 2010-11-10 22:18  星空有我  阅读(500)  评论(0编辑  收藏  举报