查找在ComposeParts调用之后创建的对象的导出 [英] Locating Exports for Objects Created After ComposeParts Called

查看:216
本文介绍了查找在ComposeParts调用之后创建的对象的导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的应用程序,具有相同类型的单个导出和多个导入.

I've got a very simple app with a single export and multiple imports for the same type.

在调用ComposeParts之后,我可以看到导入在我从中调用ComposeParts的同一类中进行了-MyService属性已连接.

After I call ComposeParts I can see that the import worked in the same class where I called ComposeParts from - the MyService property has been hooked up.

问题是我有另一个需要访问MyService的UserControl,并且未设置该属性-它在同一包中,等等.但是在我调用ComposeParts时并没有被实例化.

The problem is that I have another UserControl that needs access to MyService and the property isn't set - it's in the same package etc. but it's not instatiated at the time I call ComposeParts.

如果我将我的CompositionContainer设为公共/静态,并调用ComposeParts并传递UserControl,则会设置MyService属性,但这是一个可怕的解决方案.

If I make my CompositionContainer public / static and call ComposeParts and pass the UserControl the MyService property is set but that's a horrible solution.

任何人都可以对正在发生的事情有所了解吗?是ComposeParts足够聪明以挂接现有对象吗?Import属性是否可以在以后的对象上使用?我没有正确地挂钩吗?

Can anyone shed some light on what's going on? Is ComposeParts only smart enough to hook up existing objects or will an Import attribute be able to work on objects at a later time? Did I hook something up incorrectly?

public partial class App : Application    
{

  protected override void OnActivated(EventArgs e)
  {
    AssemblyCatalog assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
    compositionContainer = new CompositionContainer(assemblyCatalog);
    compositionContainer.ComposeParts(this);
  }

  [Import(typeof(MyService))]
  public MyService MyService { get; set; }
}

更新:

我正在尝试将25万行C#项目从.Net 2.0中的服务提供商模型升级到MEF.

Update:

I'm trying to upgrade a 250k line C# prject from the Service Provider Model found in .Net 2.0 to MEF.

似乎您不能仅通过Import属性将新的对象实例自动连接到其所需的服务.似乎您需要重新触发ComposeParts或类似的东西. me脚.

It doesn't seem like you can have a new object instance automatically be hooked up to the services it requires solely through the Import attribute. It seems like you need to re-trigger the ComposeParts or something like that. Lame.

在.Net 2.0提供程序/容器模型中,您需要将子对象显式添加到父容器,而找到服务将是从子容器到父容器的递归检查.我不确定MEF中的杂物是什么?

In the .Net 2.0 provider/container model you needed to explicitly add child objects to parent containers and finding a service would be a recursive check from children to parents containers. I'm not sure what the corrolary is in MEF??

推荐答案

看起来像是一个问题的事情是,您的目录中只有一个程序集(正在执行的程序集).如果这是一个单个程序集项目,其中所有[Export]项都在同一程序集中,则它将正常工作.如果不是,则需要将所有程序集传递到目录中或使用DirectoryCatalog.

One thing that looks like an issue is that you have only one assembly in your catalog (the executing assembly). If this is a single-assembly project where all the [Export] items are in the same assembly, then it will work fine. If not, then you'll need to pass all the assemblies into the catalog or use a DirectoryCatalog.

可以用[Export]标记UserControl类,然后可以使用CompositionContainer.GetExportedValue()创建UserControl并一次性满足其所有[Import]需求,而不用调用其构造函数.但是,如果表单在设计时已经在其中具有控件,则对于UI而言,这并不总是可行的.在这种情况下,您必须调用ComposeParts来设置[Import]值.

You can mark the UserControl classes with [Export], then instead of calling their constructors, you can use CompositionContainer.GetExportedValue() to create the UserControl and fulfill all its [Import] needs in one go. However, this is not always feasible with UI's if the forms already have the controls on there at design-time. In this case, you'll have to call ComposeParts to setup the [Import] values.

这实际上归结为您如何在应用程序中设置UserControl类.如果在运行时进行连接,则有机会挂接到CompositionContainer来为您创建实例并自动进行连接.如果您依靠设计时代码来设置控件,则需要将所有GetService()调用替换为ComposeParts()调用.

It really boils down to how you are setting up the UserControl classes in the application. If you wire it up at runtime, then you have a chance to hook into the CompositionContainer to create the instances for you and wire them up automatically. If you are relying on design-time code to set up the controls, then you will need to replace all your GetService() calls with ComposeParts() calls.

这篇关于查找在ComposeParts调用之后创建的对象的导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆