[英]How does MEF's DirectoryCatalog work?
DirectoryCatalog
扫描DirectoryCatalog
的程序集以确定导入/导出的类。 未加载任何没有导入/导出的程序集。
这是一个很棒的功能,但它是如何工作的? 要调查程序集中类型的自定义属性,是否需要加载程序集? 一旦加载,它就无法卸载,因此不能如何工作。
它在做某种AppDomain魔术吗?
试试看。 DirectoryCatalog
只是为给定目录中的每个.dll文件创建一个AssemblyCatalog
。 因为AssemblyCatalog
调用AssemblyName.GetAssemblyName
,所以不会加载非.NET .dll文件(抛出异常并在AssemblyCatalog
捕获)。 AssemblyCatalog
调用Assembly.Load
上AssemblyName
它创建。 因此,在创建DirectoryCatalog
时会立即加载程序集。 没有魔法,没有AppDomains
。 但是众所周知, MEF
将程序集加载到当前的AppDomain
。 如果需要可卸载的程序集,请使用MAF 。
这是可以帮助您的示例代码。
var directoryPath = "Dll folder path";
//Load parts from the available dlls in the specified path using the directory catalog
var directoryCatalog = new DirectoryCatalog(directoryPath, "*.dll");
//Creating an instance of aggregate catalog. It aggregates other catalogs
var aggregateCatalog = new AggregateCatalog();
aggregateCatalog.Catalogs.Add(directoryCatalog);
//Crete the composition container
var container = new CompositionContainer(aggregateCatalog);
container.ComposeParts(this);
[ImportMany]
public List<IModule> Modules { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.