来自对象的 MEF 元数据字典

发布于 2024-09-28 16:24:43 字数 334 浏览 0 评论 0 原文

我想手动创建组合导出,但 Export 构造函数仅接受元数据字典,而我应该直接从元数据对象 (IMyMetadata) 创建它。 没有办法从对象创建字典吗?像这样:

IDictionary<string, object> metadata = AttributedModelServices.GetMetadataDictionary(metadata)

其中元数据是 IMyMetadata 对象。

本质上,我的需要是使用导入的 IEnumerable>要创建新的合成,请将值“转发”到另一个 CompositionBatch。

谢谢

I would like to manually create exports for composition, but the Export constructor only accepts a dictionary for metadata, while I should directly create it from the metadata object (IMyMetadata).
Isn't there a way to create the dictionary from the object? Something like this:

IDictionary<string, object> metadata = AttributedModelServices.GetMetadataDictionary(metadata)

where metadata is an IMyMetadata object.

Essentially, my need is that of using an imported IEnumerable> to create new compositions, sort of "forwarding" values to another CompositionBatch.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

盛装女皇 2024-10-05 16:24:43

有没有办法创建
来自对象的字典?

当然可以,像这样:

  public static IDictionary<string, object> GetPropertiesDictionary<T>(object o)
  {
     var dictionary = new Dictionary<string, object>();
     foreach (var property in typeof(T).GetProperties())
     {
        dictionary[property.Name] = property.GetValue(o,null);
     }
     return dictionary;
  }

本质上,我的需要是使用
导入的 IEnumerable 来创建新的
作曲,有点“转发”
值到另一个 CompositionBatch

听起来您正在尝试实现某种 "ExportMany" 功能, MEF 中(还?)不存在。 (它确实存在于其他一些 IOC 容器中,例如 AutoFac 中的轻量级适配器)。您也许可以不需要这样的功能。

我假设您有某种类型的导出 (IStuff) 可用,但您需要将它们包装在适配器中,以便可以将它们作为其他类型导入 (IAdaptedStuff) )。也许您可以坚持正常的属性编程模型并执行以下操作:

[Export(typeof(IAdaptedStuffUser))]
public class AdaptedStuffUser : IAdaptedStuffUser
{
   [Import] // instead of [ImportMany(typeof(IAdaptedStuff)))]
   public IAdaptedStuffProvider AdaptedStuffProvider { private get; set; }

   public void DoSomething()
   {
      foreach (var adaptedStuff in AdaptedStuffProvider.GetAdaptedStuff())
      {
         ...
      }
   }
}

[Export(typeof(IAdaptedStuffProvider))]
public AdaptedStuffProvider : IAdaptedStuffProvider
{
   [ImportMany]
   public IEnumerable<IStuff> StuffToAdapt { get; set; }

   public IEnumerable<IAdaptedStuff> GetAdaptedStuff()
   {
      return StuffToAdapt.Select(x => new Adapter(x));
   }
}

Isn't there a way to create the
dictionary from the object?

Sure, like this:

  public static IDictionary<string, object> GetPropertiesDictionary<T>(object o)
  {
     var dictionary = new Dictionary<string, object>();
     foreach (var property in typeof(T).GetProperties())
     {
        dictionary[property.Name] = property.GetValue(o,null);
     }
     return dictionary;
  }

Essentially, my need is that of using
an imported IEnumerable to create new
compositions, sort of "forwarding"
values to another CompositionBatch

It sounds like you're trying to implement sort of a "ExportMany" feature, which does not (yet?) exist in MEF. (It does exist in some other IOC containers, like lightweight adapters in AutoFac). You may be able to do without such a feature.

I'm going to assume you have exports of some type (IStuff) available, but you need to wrap them in adapters so that you can import them as something else (IAdaptedStuff). Maybe you could stick to the normal attributed programming model and do something like this:

[Export(typeof(IAdaptedStuffUser))]
public class AdaptedStuffUser : IAdaptedStuffUser
{
   [Import] // instead of [ImportMany(typeof(IAdaptedStuff)))]
   public IAdaptedStuffProvider AdaptedStuffProvider { private get; set; }

   public void DoSomething()
   {
      foreach (var adaptedStuff in AdaptedStuffProvider.GetAdaptedStuff())
      {
         ...
      }
   }
}

[Export(typeof(IAdaptedStuffProvider))]
public AdaptedStuffProvider : IAdaptedStuffProvider
{
   [ImportMany]
   public IEnumerable<IStuff> StuffToAdapt { get; set; }

   public IEnumerable<IAdaptedStuff> GetAdaptedStuff()
   {
      return StuffToAdapt.Select(x => new Adapter(x));
   }
}
眼眸印温柔 2024-10-05 16:24:43

您可以导入 IEnumerable> 来获取原始元数据字典。听起来这对你有用。

You can import IEnumerable<IContract, IDictionary<string, object>> to get the raw metadata dictionary. It sounds like this would work for you.

零時差 2024-10-05 16:24:43

例如:

[ImportMany]    
IEnumerable<IContract, IDictionary<string, object>> MyContracts { get; set; }

For example:

[ImportMany]    
IEnumerable<IContract, IDictionary<string, object>> MyContracts { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文