MEF GetExportedValue 与元数据
我想使用 MEF 作为我的项目的 DI。我有 1 个项目,每个应该组成的类都驻留在其中(它们共享一个接口)。现在我想通过指定元数据值来创建其中之一。定义如下:
public interface IGatewayResponseReader
{
object Read(string msg);
}
[Export(typeof(IGatewayResponseReader))]
[ExportMetadata(G3Reader.META_KEY, "value1")]
public class TestReader1 : IGatewayResponseReader
{
...
}
[Export(typeof(IGatewayResponseReader))]
[ExportMetadata(G3Reader.META_KEY, "value2")]
public class TestReader2 : IGatewayResponseReader
{
...
}
现在我想通过 MEF 创建 TestReader1 的实例,但我不知道如何通过 CompositionContainer 按元数据进行过滤。我想要类似
Container.GetExportedValue<IGatewayResponseReader>();
But 的东西来指定元数据来选择要创建的类实例。
非常感谢您的帮助。
谢谢。
I want to use MEF as a DI for my project. I have 1 project and every classes that should be composed resides there (they share one interface). Now I want to create one of them by specifiyng a metadata value. Here's the definitions:
public interface IGatewayResponseReader
{
object Read(string msg);
}
[Export(typeof(IGatewayResponseReader))]
[ExportMetadata(G3Reader.META_KEY, "value1")]
public class TestReader1 : IGatewayResponseReader
{
...
}
[Export(typeof(IGatewayResponseReader))]
[ExportMetadata(G3Reader.META_KEY, "value2")]
public class TestReader2 : IGatewayResponseReader
{
...
}
Now I want to create an instance of TestReader1 through MEF, but I don't know how to filter by metadata through CompositionContainer. I want something like
Container.GetExportedValue<IGatewayResponseReader>();
But to specify the metadata to choose which class instance to create.
Your help is much appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Dmitry Ornatsky 提供的答案是正确的,但提供导出元数据的首选方法是使用自定义导出属性来使用强类型元数据:
然后可以将查找导入的代码设置为类型安全的。请注意,在访问
Value
属性之前检查 import 是否不为 null 是个好主意:The answer provied by @Dmitry Ornatsky is correct, but the preferred way of providing export metadata is to use strongly-typed metadata using a custom export attribute:
The code to look up an import can then be made type-safe. Note that it's a good idea to check if import is not null before accessing the
Value
property: