[英]MEF and DirectoryCatalog
如果目录不存在,有没有办法安全地使用 DirectoryCatalog 来处理?
这是我的容器如何设置的代码示例:
//Create an assembly catalog of the assemblies with exports
var catalog = new AggregateCatalog(
new AssemblyCatalog(Assembly.GetExecutingAssembly()),
new AssemblyCatalog(Assembly.Load("My.Second.Assembly")),
new DirectoryCatalog("Plugins", "*.dll"));
//Create a composition container
var container = new CompositionContainer(catalog);
但是,如果目录不存在,则会引发异常,我想忽略该错误。
如果抛出异常,显然不会。 只需在运行 MEF 容器设置之前创建目录,就不会抛出任何错误。
根据文档:
该路径必须是绝对的或相对于
AppDomain.BaseDirectory
。
PsuedoCode 进行目录检查:
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
//Check the directory exists
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//Create an assembly catalog of the assemblies with exports
var catalog = new AggregateCatalog(
new AssemblyCatalog(Assembly.GetExecutingAssembly()),
new AssemblyCatalog(Assembly.Load("My.Other.Assembly")),
new DirectoryCatalog(path, "*.dll"));
//Create a composition container
_container = new CompositionContainer(catalog);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.