如何在 MEF 中将类型从 typename 传递到 GetExports?
我已经创建了一个 CompositionContainer,现在我不想显式地给出类型,而是想通过使用类型的名称来获取导出。
下面的代码工作正常:
var p1Value = p.Container.GetExports<IPlugin, IPluginData>()
.First(ip => ip.Metadata.Param.Equals(
args[1],
StringComparison.OrdinalIgnoreCase))
.Value
.Execute(args.Skip(1).ToArray());
Console.WriteLine(p1Value);
但如果我有两个包含“IPlugin”和“IPluginData”的字符串变量,我想实现同样的目标。有什么方法可以按名称传递类型吗?
I have created a CompositionContainer, and now instead of giving the types explicitly I want to get the exports by using the names of the types.
The code below is working fine:
var p1Value = p.Container.GetExports<IPlugin, IPluginData>()
.First(ip => ip.Metadata.Param.Equals(
args[1],
StringComparison.OrdinalIgnoreCase))
.Value
.Execute(args.Skip(1).ToArray());
Console.WriteLine(p1Value);
But I want to achieve same thing if I have two string variables containing "IPlugin" and "IPluginData". Is there any way to pass the types by name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
警告:这不是使用 MEF 的正常方法。但既然你问了......你可以使用 GetExports 的重载接受 导入定义。
要了解必须为给定类型使用哪个合约名称,您可以调用 AttributedModelServices.GetContractName(typeof(IPlugin))。通常它只是完整的类型名称。
确切的元数据类型并不重要 - 重要的是在其上声明的元数据属性。您可以在下面的
requiredMetadata
字典中描述它们。Caveat: this isn't the normal way to use MEF. But since you asked... you can use the overload of GetExports which accepts an ImportDefinition.
To discover which contract name you have to use for a given type, you can call
AttributedModelServices.GetContractName(typeof(IPlugin))
. Typically it is just the full type name.The exact metadata type is not important - all that matters are the metadata properties declared on it. You can describe these as in the
requiredMetadata
dictionary below.