c# - 如何在不将整个 DLL 复制到内存的情况下读取 DLL 插件中的 MEF 元数据?

标签 c# plugins dll metadata mef

背景:

我对使用 MEF 在使用 C# 和 .NET 4.0 的 WinForm 应用程序中提供插件架构很感兴趣,但我不清楚一些事情。

首先:我还没有在 C# 中构建 DLL,而且我对 DLL 程序集的概念以及 DLL 通常如何加载到内存中(意思是一次性加载或分段加载)有点模糊需要)

意图:

该程序将是一个机器硬件控制框架,并将由一个主要的 WinForm GUI 组成,该 GUI 是一个具有基本工具栏、菜单等的通用环境 - 但没有大量 GUI 内容。 (想想:MDI Parent 但实际上不是)。

插件提供特定机器的所有控件。许多可能的插件中的每一个都可能包含 30 到 50 个大型 UserControl,每个都填充了数十个 WinForm 控件和构成各种机器控制面板的大量支持代码。

这意味着主程序是一个轻量级的通用框架,插件包含将在主程序用户界面中显示的大量 GUI 控件和功能,包括大量图标、图像和其他资源。这将使插件 DLL 可能变得非常大。

目标是允许用户从菜单中选择一个插件,然后在选择后加载并运行该插件,然后它将用面板、菜单和工具箱填充几乎是空的主 GUI。

为此,我需要首先从每个插件中提取元数据以填充程序的初始菜单,其中将包括插件标题、描述、图标、版本号和其他信息。

问题如下:

使用 MEF,如果我尝试从存储在插件文件夹中的许多大型 DLL 中的每一个读取元数据,是否会在访问元数据值之前将整个 DLL 复制到内存中?

如果是这样,是否有任何方法可以打开每个 DLL 文件并仅将元数据读入内存以构建初始菜单 - 然后稍后通过 MEF 加载完整的选定 DLL?

我假设用于通过 MEF 读取插件的典型 DirectoryCatalog 和 AggregateCatalog 模板会将所有发现的 DLL 复制到内存中并将它们存储在目录集合中。

DLL 是否包含一个连续的代码块(程序集),或者它们是否可以包含多个单独的 block ,这些代码块被索引并根据需要单独复制到内存(多个程序集)?

我可能不了解基本原理,并且可能会混淆术语。如果能深入了解 MEF、DLL 和程序集的一般加载行为,我将不胜感激。谢谢!

最佳答案

With MEF, if I try to read the metadata from each of many large DLLs that are stored in a plug-in folder, will the entire DLL be copied into memory before the metadata values are accessed ?

据我所知,整个 DLL 将被加载到内存中。不过,这与 MEF 没有任何关系。 DirectoryCatalog 将使用对 Assembly.Load 的调用加载程序集(通过 AssemblyCatalog) .此方法不是 MEF 的一部分,但它是 .NET Framework 的核心方法。大多数加载的程序集都是以这种方式加载的。如果您使用 Process Explorer 监控运行应用程序的进程您可以看到 Virtual Size 将随着加载程序集的大小而增加。因此,如果您加载大型程序集,您的进程的虚拟大小将会很高。

If so, is there any way to open each DLL file and only read the metadata into memory to build the initial menu - then later load the full selected DLL through MEF ?

有很多方法可以做到这一点。

一种方法是创建一个新的 application domain ,在新的 AppDomain 中创建 CompositionContainer 并检查发现的部分。然后将这些部分的信息序列化到主AppDomain中。最后卸载新的 AppDomain。然后您可以检查您真正需要哪些部件,并只加载包含它们的程序集。可以在 answer 中找到有关如何执行此操作的示例.

另一种方法是使用 Mono.Cecil .这是一个很棒的库,可以帮助您在不加载程序集的情况下检查程序集。您可以将它与 MEF's Export Metadata 结合使用在这样的方法中:

public static bool IncludesTypeWithSpecificExportMetadata<T>(string assemblyPath, string name, T value)
    {
        AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(assemblyPath);

        bool typeWasFound = false;          

        foreach (TypeDefinition typeDefinition in assemblyDefinition.MainModule.GetTypes())
        {
            foreach (CustomAttribute customAttribute in typeDefinition.CustomAttributes)
            {
                if (customAttribute.AttributeType.FullName == typeof(ExportMetadataAttribute).FullName)
                {
                    string actualName = (string)customAttribute.ConstructorArguments[0].Value;
                    T actualValue = (T)((CustomAttributeArgument)customAttribute.ConstructorArguments[1].Value).Value;
                    if (actualName.Equals(name) && actualValue.Equals(value))                        
                    {
                        typeWasFound = true;                       
                    }
                }
            }
        }

        return typeWasFound;
    }

给定程序集文件路径和名称/值对,此方法将使用 Mono.Cecil 检查程序集并查找用 ExportMetadataAttribute 修饰的类型并具有相同的名称/值对。

I am assuming that the typical DirectoryCatalog and AggregateCatalog template for reading plugins through MEF will copy all of the discovered DLLs into memory and store them in the catalog collection.

是的。

Do DLLs contain one contiguous code block ( assembly ), or can they contain multiple separate blocks that are indexed and copied to memory individually as needed ( multiple assemblies ) ?

这个我就不知道了。您可能会在 Don Box 的“Essential .NET Volume 1”或 Jeffrey Richter 的“C# via CLR”中找到答案。

I'm probably not understanding the fundamentals, and maybe confusing terms. I would appreciate any insight into the load behavior of MEF, DLLs, and Assemblies in general. Thanks !

我上面提到的书籍详细介绍了如何解析/加载程序集等等。也看看 Suzanne Cook's blog .

现在我想问你一件事。您真的需要将大文件嵌入到程序集中吗?如果你能找到另一种方法,那么你将不需要任何这些。您的插件引擎会稍微简单一些。

最后我建议看看微软的Smart Client Software Factory .它几乎可以完成您提到的所有事情,甚至更多。理解它并适应它需要付出一些努力,但从长远来看,它可能会为您节省大量时间。

关于c# - 如何在不将整个 DLL 复制到内存的情况下读取 DLL 插件中的 MEF 元数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14619052/

相关文章:

c# - 不使用字符串函数也不使用C#中的循环语句来检查字符串是否是回文

c# - 如何使用 Magick.NET 或 GraphicsMagick.NET 创建图像图 block

python - Python 2.6 中的动态类加载 : RuntimeWarning: Parent module 'plugins' not found while handling absolute import

reactjs - 如何在 React Typescript 应用程序中使用 Wavesurfer.js 插件?

c++ - LNK4098 - 使用 GLFW 的 'MSVCRT' 冲突

c# - 在字符串列表中查找与输入字符串最接近的匹配项

c# - 异常 : system. IO.FileNotFoundException : cannot find assembly or file Mysql. 数据

vim - 无法安装 vim 表格插件

c - 从 DLL 返回错误代码

c++ - VisualC++ DLL 中的 OpenSSL