首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >指定类型的CompositionBatch.AddExportedValue

指定类型的CompositionBatch.AddExportedValue
EN

Stack Overflow用户
提问于 2014-04-23 09:20:38
回答 1查看 437关注 0票数 1

我将集装箱设置如下:

代码语言:javascript
运行
复制
....
CompositionContainer container = new CompositionContainer(catalog);
CompositionBatch batch = new CompositionBatch();

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

_dataClient_orderClient在一个不同的集合中,我不能碰它们。然而,它们分别实现了IDataFeedIOrderFeed (但是它们的类型分别是DataClientOrderClient )。稍后,我在构造函数中期望它们:

代码语言:javascript
运行
复制
[ImportingConstructor]
public ShellViewModel(IShellView view, IDataFeed dataFeed, IOrderFeed orderFeed)
...

但这会抛出一个ImportCardinalityMismatchException。但是,如果我将构造函数更改为此,它可以工作:

代码语言:javascript
运行
复制
[ImportingConstructor]
public ShellViewModel(IShellView view, DataClient dataFeed, OrderClient orderFeed)
...

我尝试过这样做,但也提出了同样的例外:

代码语言:javascript
运行
复制
...
batch.AddExportedValue(typeof(IDataFeed).FullName, _dataClient);
batch.AddExportedValue(typeof(IOrderFeed).FullName, _orderClient);
...

如何将_dataClient_orderClient添加到容器中,就好像我这样做了一样:

代码语言:javascript
运行
复制
[Export(typeof(IDataFeed))]
public class DataClient : IDataFeed
{
    ...

我该怎么做?理想的情况是这样的:

代码语言:javascript
运行
复制
batch.AddExportedValue(typeof(IDataFeed), _dataClient);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-23 09:51:39

解决办法很简单。不知道我怎么错过的。

代码语言:javascript
运行
复制
batch.AddExportedValue((IDataFeed)_dataClient);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23240002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档