문제

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);
도움이 되었습니까?

해결책

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

batch.AddExportedValue((IDataFeed)_dataClient);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top