简体   繁体   English

MEF 和目录目录

[英]MEF and DirectoryCatalog

Is there a way to safely use DirectoryCatalog to handle if the directory doesn't exist?如果目录不存在,有没有办法安全地使用 DirectoryCatalog 来处理?

Here a code example of how my container is setup:这是我的容器如何设置的代码示例:

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

But an exception is thrown if the directory doesn't exist, and I would like to ignore that error.但是,如果目录不存在,则会引发异常,我想忽略该错误。

Apparently not if an exception is thrown.如果抛出异常,显然不会。 Just create the directory prior to running the MEF container setup and then no error will be thrown.只需在运行 MEF 容器设置之前创建目录,就不会抛出任何错误。

According to the documentation :根据文档

The path must be absolute or relative to AppDomain.BaseDirectory .该路径必须是绝对的或相对于AppDomain.BaseDirectory

PsuedoCode to do a directory check: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM