物业进口什么时候满意?

时间:2013-08-13 11:56:04

标签: c# mef

何时满足房产进口?我认为它们会在构造函数之前得到满足,因为属性在构造函数运行之前被初始化,但是下面的示例在构造函数中将ImportedClass显示为null。

我知道我可以使用ImportingConstuctor来解决这个问题。这是为了理解何时满足房产进口。

public MyClass
{
  [Import]
  public ImportedClass ImportedClass {get;set;}

  public MyClass()
  {
      //Imported Class is null at this point, so nothing can be done with it here.
  }
}

1 个答案:

答案 0 :(得分:6)

在调用构造函数之前,无法操纵对象。 MEF通过名为IPartImportsSatisfiedNotification

的界面为您的问题提供解决方案
public MyClass : IPartImportsSatisfiedNotification
{
  [Import]
  public ImportedClass ImportedClass {get;set;}

  public MyClass()
  {
      //Imported Class is null at this point, so nothing can be done with it here.
  }

  public void OnImportsSatisfied() 
  {
     //ImportedClass is set at this point.
  }
}

关于MEF设置导入的操作;它首先调用构造函数,然后设置任何属性,然后调用通知方法。