MEF:使用 GetExports 时元数据似乎覆盖接口
我正在构建一个基于 MEF 的以插件为中心的 WPF 应用程序,我遇到了 GetExports 的问题,也许这只是我的无知,但我发现了一个奇怪的行为。我有许多导出的部件,全部派生自 2 个不同的接口(让我们将它们命名为 A 和 B),但都标记有相同的元数据属性 X。所以我有如下代码:
[Export(typeof(A))] [X属性在这里...] public class SomePart1 : 每个部分都有一个 { ... }
,对于实现 B 的类也是如此:
[Export(typeof(B))] [X属性在这里...] public class SomePart2 : B { ... }
现在,当我尝试获取所有实现 A 并由属性 X 用某些值修饰的部分时,MEF 不仅返回 A 实现部分,还返回B-执行部件。因此,当我期望处理 A 对象时,我得到了 B,从而出现了强制转换异常。
在现实世界中,接口被命名为 IItemPartEditorViewModel 和 IItemPartEditorView,而它们的公共属性被命名为 ItemPartEditorAttribute 并公开一个 PartType 字符串属性,我可以对其进行一些过滤。我获取零件的代码如下:
var p = (from l in container.GetExports<IItemPartEditorViewModel, IItemPartEditorMetadata>()
where l.Metadata.PartType == sPartType
select l).FirstOrDefault();
当查找 PartType 等于某个值的 IItemPartEditorViewModel 时,我得到 IItemPartEditorView 而不是 IItemPartEditorViewModel 实现对象。如果我注释掉 IItemPartEditorView 对象中的属性,我将正确获取 IItemPartEditorViewModel 实现对象。
更新使用了建议的“模板化”方法,但我在这里输入错误,因为我忘记将小于和大于更改为实体。无论如何,查看代码我注意到在属性中我有“ViewModel”而不是“View”作为接口类型,所以这就是问题所在。我真丢脸,抱歉打扰了:)!
I'm building a MEF-based plugin-centric WPF application and I'm facing an issue with GetExports, maybe it's just my ignorance but I find an odd behaviour. I have a number of exported parts, all derived from 2 different interfaces (let's name them A and B), but all marked with the same metadata attribute X. So I have code like:
[Export(typeof(A))]
[TheXAttributeHere...]
public class SomePart1 : A { ... }
for each part, and the same for classes implementing B:
[Export(typeof(B))]
[TheXAttributeHere...]
public class SomePart2 : B { ... }
Now, when I try getting all the parts implementing A and decorated by attribute X with some values, MEF returns not only the A-implementing parts, but ALSO the B-implementing parts. So, when I expect to deal with A-objects I get a B, whence a cast exception.
In the real world, interfaces are named IItemPartEditorViewModel and IItemPartEditorView, while their common attribute is named ItemPartEditorAttribute and exposes a PartType string property on which I do some filtering. My code to get parts is thus like e.g.:
var p = (from l in container.GetExports<IItemPartEditorViewModel, IItemPartEditorMetadata>()
where l.Metadata.PartType == sPartType
select l).FirstOrDefault();
When looking for IItemPartEditorViewModel whose PartType is equal to some value, I get the IItemPartEditorView instead of IItemPartEditorViewModel implementing object. If I comment out the attribute in the IItemPartEditorView object instead, I correctly get the IItemPartEditorViewModel implementing object.
Update the suggested "templated" method was used, but I mistyped it here as I forgot to change lessthan and greaterthan into entities. Anyway, reviewing the code I noticed that in the attribute I had "ViewModel" instead or "View" for the interface type, so this was the problem. Shame on me, sorry for bothering :)!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我需要查看更多代码才能确定发生了什么。但是,我建议您像这样调用 GetExports:
然后对返回的列表进行过滤。这可能会解决您遇到的演员问题。我也有兴趣查看自定义元数据属性的代码。例如,如果它派生自 ExportAttribute,则可能是问题的一部分。
I think I'd need to see more of the code to know for sure what's going on. However, I'd suggest you call GetExports like this:
Then do your filtering on the list returned. This will probably fix the cast issues you are having. I'd also be interested in seeing the code for the custom metadata attribute. If it derives from ExportAttribute for example, that might be part of the problem.