Question

I set up the container as follows:

....
CompositionContainer container = new CompositionContainer(catalog);
CompositionBatch batch = new CompositionBatch();

batch.AddExportedValue(_dataClient);
batch.AddExportedValue(_orderClient);
batch.AddExportedValue(container);
container.Compose(batch);

_dataClient and _orderClient are in a different assembly and I can't touch them. However they implement IDataFeed and IOrderFeed respectively (However their types are DataClient and OrderClient respectively). Later I expect them in a constructor:

[ImportingConstructor]
public ShellViewModel(IShellView view, IDataFeed dataFeed, IOrderFeed orderFeed)
...

But this throws an ImportCardinalityMismatchException. However if I change the constructor to this it works:

[ImportingConstructor]
public ShellViewModel(IShellView view, DataClient dataFeed, OrderClient orderFeed)
...

I tried this but the same exception was raised:

...
batch.AddExportedValue(typeof(IDataFeed).FullName, _dataClient);
batch.AddExportedValue(typeof(IOrderFeed).FullName, _orderClient);
...

How can I add _dataClient and _orderClient to the container as if I did this:

[Export(typeof(IDataFeed))]
public class DataClient : IDataFeed
{
    ...

How do I do this? Ideally something like this:

batch.AddExportedValue(typeof(IDataFeed), _dataClient);
Was it helpful?

Solution

The fix was easy. Don't know how I missed that.

batch.AddExportedValue((IDataFeed)_dataClient);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top