views:

43

answers:

1

Take a look at this code :

public class Program
{
[import]IMain Main {get; set;}
...
private Compose() {...}
}

internal interface IMain
{
...
}

[Export(typeof(IMain)]
public class Main : IMain
{
  [import]
  Interace1 Object1 {get;set;}

  [import]
  Interace2 Object2 {get;set;}
}
...

I want to lazy load Object2 after the composition beween Program and Main. When I compose in Program, I've a MEF error because MEF try to compose Object2 as well (but the implementation of Interface2 is not available at the begining... I want to load it after).

How can I do this ?

I've tried to put :

 [import]
  Lazy<Interace2> Object2 {get;set;}

but I still have the same trouble.

+2  A: 

Use this:

[Import(AllowDefault=true, AllowRecomposition=true)]
Lazy<Interface2> Object2 {get;set;}

AllowDefault will allow the composition to succeed when there is no Interface2, and AllowRecomposition will allow you to add it later. It doesn't matter if you use a property of type Lazy<Interface2> or just Interface2 in this case.

Daniel Plaisted