如何通过MEF获取导入的UserControl的程序集信息,而不实例化UserControl?

发布于 2024-10-13 07:26:45 字数 440 浏览 2 评论 0原文

是否可以从导入的 MEF 类型获取程序集信息,无需实例化类型?我需要知道包含类型的插件控件的程序集名称和版本。尝试了以下方法,但它只返回 System.ComponentModel.Composition。

foreach (Lazy<UserControl, IMetadata> content in contents)
{
    // get assembly information of the Plugin control for the imported function 
    string definingAssembly = content.GetType().Assembly.GetName();
    Console.WriteLine(definingAssembly);
}

为了尽可能避免维护问题,我想避免在元数据中指定此信息。

Is it possible get the assembly information from an imported MEF Type, without instantiating a type? I need to know the assembly name and version of the Plugin control that contains the Type. Tried the following, but it just returns the System.ComponentModel.Composition.

foreach (Lazy<UserControl, IMetadata> content in contents)
{
    // get assembly information of the Plugin control for the imported function 
    string definingAssembly = content.GetType().Assembly.GetName();
    Console.WriteLine(definingAssembly);
}

To avoid maintenance issues if possible I want to avoid specifying this information in Metadata.

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

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

发布评论

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

评论(3

似梦非梦 2024-10-20 07:26:45

这是 MEF 论坛上时不时弹出的常见问题解答 。 (我确信有几个更好的线程包含答案,但我无法立即找到一个)。

MEF 不支持开箱即用地检查延迟导入(不实例化该部件)的部件类型。这可能是由于其设计理念中的两个要素:

  1. MEF 鼓励松散耦合。您应该根据合同指定和使用导入。依赖于导入的实际类类型将阻止您将来更改组合。

  2. MEF 允许延迟加载类型。假设有一个 Lazy.PartType 属性,您可以使用它来检查部件的实际类型。这将强制加载零件类型和包含的组件。然后,您根据 PartType 决定您不想实例化该特定部件,并且程序集加载毫无意义。

    (据我所知,目前 MEF 尚未利用开箱即用的第二个设计功能。无论如何,当 AssemblyCatalog.Parts 被调用时,AssemblyCatalog 将立即检查所有类型。但是有MEF 源代码中的示例显示了如何缓存有关程序集的信息以延迟或避免程序集加载。)

因此,您不应依赖零件的确切类型,而应添加一些可用于选择导入的元数据。

This is a FAQ that pops up on the MEF forum every now and then. (I'm sure that there are several better threads that include an answer, but I'm unable to find one immediately).

MEF does not support inspecting the part type of a lazy import (without instantiating that part) out of the box. This is probably because of two elements in its design philosophy:

  1. MEF encourages loose coupling. You should specify and use imports based on their contract. Relying on the actual class type of an import would prevent you from changing the composition in the future.

  2. MEF allows for lazy loading of types. Suppose there would be a Lazy.PartType property which you can use to inspect he actual type of the part. This would force the part type and the containing assembly to be loaded. Then you decide based on the PartType that you don't want to instantiate that particular part, and the assembly load was for nothing.

    (As far as I can tell, currently MEF does not yet exploit the second design feature out of the box. AssemblyCatalog will inspect all types immediately when AssemblyCatalog.Parts is called anyway. But there is a sample in the MEF sources that shows how to cache information about assemblies to delay or avoid assembly loads.)

So instead of relying on the exact type of the part, you should add some metadata that you can use to do the selection of an import.

行雁书 2024-10-20 07:26:45

您尝试的操作应该会失败,因为程序集永远不可能是动态加载的模块程序集。

我想如果您不实例化类型,该模块程序集甚至不会加载到内存中。

您是否尝试过在 MEF 讨论区发帖?在那里您可能会见到开发人员。

http://mef.codeplex.com/discussions

What you tried should fail, as the Assembly never can be the dynamically loaded module assembly.

I guess if you don't instantiating a type, that module assembly is not even loaded into memory.

Did you try to post to MEF discussion board yet? There you may meet the developers.

http://mef.codeplex.com/discussions

浅笑依然 2024-10-20 07:26:45

我一直在思考这个问题,因为我自己也遇到过这个问题。我想按需创建实例,而不是让组合为我创建实例。

我遇到的情况是,我不需要知道特定类型,但我可能需要创建该类型的多个实例,使用它们,并在适当的时候处置它们。我提出的解决方案是使用 MEF 加载知道如何创建我想要控制生命周期的类型实例的工厂,而不是使用 MEF 加载具体类型并强制它为我创建实例。

MEF 将加载并创建工厂,然后我可以使用该工厂来创建可以随意使用和处置的实例。

I've been thinking about this problem as I have run into this issue myself. Where I want to create the instances on demand rather than have them created for me by the composition.

The scenario I have is that I don't need to know the particular type but I may need to create multiple instances of the type, use them, and dispose them when appropriate. The solution I came up with is instead of using MEF to load the concrete types and be forced into having it create instances for me, use MEF to load factories that know how to create instances of the type I want to control the lifetime.

MEF would load and create the factory and then I can use the factory to create instances that I can use and dispose of at will.

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