When are Own Imports Satisfied?

When are import properties saved? I thought they would be satisfied before the constructor, since the properties are initialized before the constructor starts, but the following example shows that ImportedClass is null in the constructor.

I know that I can solve this problem with ImportingConstuctor; it is necessary to understand when the import of properties is satisfied.

 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. } } 
+4
source share
1 answer

An object cannot be manipulated until its constructor is called. MEF offers a solution for your problem, but with the IPartImportsSatisfiedNotification interface

 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. } } 

About the actions taken by MEF to install your import; it first calls the constructor, then sets any properties, and then calls the notification method.

+6
source

Source: https://habr.com/ru/post/1496786/


All Articles