System.Action的 Lambda 表达式示例对于 MEF 案例?

发布于 2024-08-19 01:16:00 字数 1097 浏览 5 评论 0原文

我是 System.Action的新手和 Lambda 表达式。这是我想使用的一个案例。

using System;
using System.ComponentModel.Composition;
public class MyClass {
   public static CompositionContainer Container = new CompositionContainer();

   private void Initialize(Action<CompositonBatch> action) {}

   public MyClass() {
      CompositionBatch batch = null;
      inititialize(x=> {
          // create catalog instances: instance1 and instance2 as example
          // ...
          x.AddPart(instance1);
          x.AddPart(instance2);
          batch = x;
       });
       // at this point, will be batch be none-null value will parts added?

       // the following code is composing batch to the container
       Container.Compose(batch);
   }
}

基本上,方法 Initialize(Actionaction) 用于将 MEF 目录部件初始化为 CompositionBatch 实例,该实例添加所有导入和导出部件。之后,批处理被组合到容器中以解析所有 DI 映射。

我不确定我是否使用 System.Action和 Lambda 表达式在这里正确。在此示例中,x 会由 Composition() CTOR 即时创建吗?我应该在Initialize() 方法中添加任何内容吗?或者我应该创建一个委托作为 Initialize() (如果是这样,我认为我仍然需要将它绑定到一个方法)?

I am new to System.Action<T> and Lambda expression. Here is one case I would like to use.

using System;
using System.ComponentModel.Composition;
public class MyClass {
   public static CompositionContainer Container = new CompositionContainer();

   private void Initialize(Action<CompositonBatch> action) {}

   public MyClass() {
      CompositionBatch batch = null;
      inititialize(x=> {
          // create catalog instances: instance1 and instance2 as example
          // ...
          x.AddPart(instance1);
          x.AddPart(instance2);
          batch = x;
       });
       // at this point, will be batch be none-null value will parts added?

       // the following code is composing batch to the container
       Container.Compose(batch);
   }
}

Basically, the method Initialize(Action<CompositionBatch> action) is used to initialize MEF catalog parts to a CompositionBatch instance, which adds all the import and export parts. After that, the batch is composed to the container to resolve all the DI mappings.

I am not sure if I use System.Action<T> and Lambda expression correctly here. Would x be created by Composition() CTOR on-fly in this example? Should I put anything in the method Initialize()? Or should I create a delegate as Initialize() instead(if so I think I still need to bind it to a method)?

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

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

发布评论

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

评论(1

习惯成性 2024-08-26 01:16:00

这里的问题是您从未调用 lambda 表达式。为此,您需要像这样更改 Initialize() 方法:

private void Initialize(Action<CompositonBatch> action) {action(new Composition());}

请注意现在如何实际调用传递给函数的方法。另外,您的构造函数中存在拼写错误(初始化而不是初始化),并且我没有看到 instance1instance2 的声明位置。

但我不确定你在这里是否真的有所收获。另请注意,这将在 batch 变量上创建一个闭包。

The problem here is that you never invoke your lambda expression. To do that, you need to change your Initialize() method like this:

private void Initialize(Action<CompositonBatch> action) {action(new Composition());}

Note how now you actually call the method you pass to the function. Also, there's a typo in your constructor (intialize rather than Initialize) and I'm not seeing where instance1 and instance2 are declared.

But I'm not sure you really gain anything here. Also be warned that this will create a closure over the batch variable.

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