如何在 MEF 中将类型从 typename 传递到 GetExports?

发布于 2024-12-03 04:45:50 字数 439 浏览 2 评论 0原文

我已经创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满身野味 2024-12-10 04:45:50

警告:这不是使用 MEF 的正常方法。但既然你问了......你可以使用 GetExports 的重载接受 导入定义

要了解必须为给定类型使用哪个合约名称,您可以调用 AttributedModelServices.GetContractName(typeof(IPlugin))。通常它只是完整的类型名称。

确切的元数据类型并不重要 - 重要的是在其上声明的元数据属性。您可以在下面的 requiredMetadata 字典中描述它们。

var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);

string contractName = "SomeNamespace.IPlugin";
var requiredMetadata = new Dictionary<string, Type>();
requiredMetadata["Meta1"] = typeof(string);
requiredMetadata["Meta2"] = typeof(int);

var importDefinition = new ContractBasedImportDefinition(
   contractName,
   null,
   requiredMetadata,
   ImportCardinality.ZeroOrMore,
   false,
   true,
   CreationPolicy.Any);

var exports = container.GetExports(importDefinition);
Console.WriteLine(exports.Count());
Console.ReadKey();

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.

var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);

string contractName = "SomeNamespace.IPlugin";
var requiredMetadata = new Dictionary<string, Type>();
requiredMetadata["Meta1"] = typeof(string);
requiredMetadata["Meta2"] = typeof(int);

var importDefinition = new ContractBasedImportDefinition(
   contractName,
   null,
   requiredMetadata,
   ImportCardinality.ZeroOrMore,
   false,
   true,
   CreationPolicy.Any);

var exports = container.GetExports(importDefinition);
Console.WriteLine(exports.Count());
Console.ReadKey();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文