Consider the following situation:
[Export]
class A { }
class B
{
[Import]
private A a;
}
// Instantiates class B.
class C
{
public C(Type type){ /*Instantiate Class B here.*/}
public void PerfomOperationUsingClassB() { }
}
class D
{
void Initialize()
{
var catalog = new AssemblyCatalog(Assembly.GetAssembly(typeof(A)));
var container = new CompositionContainer(catalog);
// Is there any way to compose A with B?
C c = new C(typeof(B));
c.PerfomOperationUsingClassB();
}
}
Problem: I have an access to class “A” and “B” but class “C” is located in the assembly that I can’t modify (thus can’t modify the class “C”). Is there any way to compose “A” and “B”?
Unfortunately, without access to whatever the constructor of
Cis doing, you pretty much out of luck. The constructor ofCseems to want to initialise an instance ofBusing the type itself… does it provide any mechanism of intercepting the initialisation ofBwithinC?If
Cwere to accept an instance ofBinstead, you could easily just pass in a composed instance ofBinto the constructor. If you wanted to export the closed type,C, you could do something like:(This is based on Mark Seemann’s Resolving closed types article).
With that, should
Cactually look like:You could have that closed type satisfied by an automatic creation of
B: