如何使用 MEF(托管扩展性框架)从目录加载 dll

发布于 2024-07-22 05:52:35 字数 1198 浏览 7 评论 0原文

我目前正在使用 MEF 并面临一些问题,

我想要的是从目录加载 dll。

首先,我扫描目录并将两个内容保存在

各自 DLL 的字典名称属性中(作为字符串)

和模块名称(作为字符串),

这里是 ScanDirectory() 代码

private void ScanPluginDirectory()
{
    catalog = new AggregateCatalog();

    catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\plugin"));            
    container = new CompositionContainer(catalog);

    batch = new CompositionBatch();
    batch.AddPart(this);        

    container.Compose(batch);    

    pluginDictionary = new Dictionary<String, String>();
    foreach (IFilter filter in filters)
    {
        Type t = filter.GetType();
        pluginDictionary.Add(filter.Name, t.Module.Name);
    }
}

,并在复选框列表中显示它们的名称。 从复选框中选择 dll 后。

我有导入语句,因为

[Import]
public IEnumerable<IFilter> filters { get; set; }

目前我的程序运行良好。 我所做的是当我从复选框列表中检查插件时。 它将其移动到“loaded”目录中,并且 QueryPlugin() 方法会查找“loaded”目录以搜索插件。

从复选框列表中取消选中插件后。 我将它移出“已加载”目录...

我想要的是使用batch.RemovePart()方法来摆脱这种将dll从一个目录快速移动到另一个目录的情况...

注意:我没有手动添加插件批量使用

batch.AddPart(new DemoFilter1());

而不是这个我使用 DirectoryCatalog();;

i am currently working with MEF and facing some problems

what i want is to load dlls from the directory.

first i scan the directory and save two things in dictionary

Name property from respective DLL (as string)

and Module Name (as string)

here is ScanDirectory() code

private void ScanPluginDirectory()
{
    catalog = new AggregateCatalog();

    catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\plugin"));            
    container = new CompositionContainer(catalog);

    batch = new CompositionBatch();
    batch.AddPart(this);        

    container.Compose(batch);    

    pluginDictionary = new Dictionary<String, String>();
    foreach (IFilter filter in filters)
    {
        Type t = filter.GetType();
        pluginDictionary.Add(filter.Name, t.Module.Name);
    }
}

and show their name in a checkbox list. upon selection of dll from checkbox.

i have import statement as

[Import]
public IEnumerable<IFilter> filters { get; set; }

currently my program is running fine. what i did is when i check a plugin from checkbox list. it moves it into "loaded" directory and and they QueryPlugin() method looks into "loaded" directory to search for plugins.

upon unchecking plugin from checkbox list. i move it out of "loaded" directory...

What i want is to use batch.RemovePart() method to get rid of this rapid moving of dlls from one directory to other....

NOTE: i am not adding plugins manually into batch using

batch.AddPart(new DemoFilter1());

instead of this i used DirectoryCatalog();;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间海 2024-07-29 05:52:35

不要使用 DirectoryCatalog,而是使用 AggregateCatalog 并将目录中每个程序集的 AssemblyCatalog 添加到聚合目录。 然后,当选中或取消选中某个插件时,您可以在 AggregateCatalog 中添加或删除相应的 AssemblyCatalog。

请注意,如果给定程序集中有多个插件,则此方法可能会出现问题。 更强大的方法可能是使用过滤目录。

Instead of using a DirectoryCatalog, use an AggregateCatalog and add an AssemblyCatalog for each assembly in the directory to the aggregate catalog. Then when a plugin is checked or unchecked, you can add or remove the corresponding AssemblyCatalog to the AggregateCatalog.

Note that there may be problems with this approach if there is more than one plugin in a given assembly. A more robust approach could be filtering individual part definitions using a filtered catalog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文