定制MEF
我遇到这种情况,我想在我正在进行的国际象棋项目中使用 MEF。假设我有一个类构造函数,如下所示:
public class MoveManager
{
private Piece _piece;
public MoveManager(Piece piece)
{
_piece = piece;
}
Mode code here...
}
在这种情况下,我将有几个从 Piece 派生的类,例如 Pawn、Rook 等。如果我将导出属性放在从 Piece 派生的所有类上,则对象将被传递到构造函数为空。 MEF 循环遍历所有具有 [Export(typeof(Piece))]
的类,如果超过 1,则传入 null。所以我不能以这种方式使用MEF。我将使用抽象工厂来获得正确的部分。似乎 MEF 的 DI 部分只能采用具有 [Export(typeof(some base class))]
的单个类。
有人能解释一下吗?
I have this situation where I want to use MEF in a chess project I'm workin on. Let's say I have a class constructor as in:
public class MoveManager
{
private Piece _piece;
public MoveManager(Piece piece)
{
_piece = piece;
}
Mode code here...
}
In this context I would have several classes that would derive from Piece like, Pawn, Rook, etc. If I put export attributes on all the classes the derive from Piece, the object being passed into the constructor is null. MEF loops through all classes the have the [Export(typeof(Piece))]
and if it exceeds 1, it passes in null. So I cannot use MEF in this way. I'm going to use an Abstact Factory for getting the correct piece. Seems like the DI part of MEF can only take a single class that has the [Export(typeof(some base class))]
.
Can anyone shed some light on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您可能正在寻找
[Importing Constructor]
Arrtibute,它告诉 MEF 如何使用导出类的构造函数。这要求将
ISomeDependency
导出到其他地方(仅一次),并接受任意数量的也可能导出的IOtherDependency
。假设您对每件作品都这样做,那么您可以:
I think you might be looking for the
[Importing Constructor]
arrtibute, which tells MEF how to use an exported class's constructor.This requires that an
ISomeDependency
is exported elsewhere (exactly once) and accepts any number ofIOtherDependency
's that might be exported too.Supposing you did this with each piece, you could then: