- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
背景:
我对使用 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/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!