如何在c#lambda语法中编写以下回调延续?

时间:2011-09-21 15:53:59

标签: c# lambda

我正在编写一个异步单元测试,我想使用lambdas(或匿名方法?)将它串起来,所以我不必为continuation定义命名函数。

我已经阅读了几篇关于lambdas的帖子,但most these处理了我不感兴趣的每个样式构造。

我想做以下事情(摘自here):

using Microsoft.Silverlight.Testing;  
using Microsoft.VisualStudio.TestTools.UnitTesting;  
{  
    [TestClass]  
    public class Test2 : SilverlightTest  
    {  
        [TestMethod]  
        [Asynchronous]  
        public void TestAsync1()  
        {  
            var eventRaised = false;  
            var result = false;  
            var timer = new System.Windows.Threading.DispatcherTimer();  

            timer.Interval = TimeSpan.FromSeconds(2);  
            timer.Tick += (object sender, EventArgs e) =>  
                {  
                    timer.Stop();  

                    // Simulate an expected result  
                    result = true;  

                    // Mark the event has being raised  
                    eventRaised = true;  
                };  

            // Start timer  
            EnqueueCallback(() => timer.Start());  

            // Wait until event is raised  
            EnqueueConditional(() => eventRaised);  
            EnqueueCallback(() =>  
            {  
                // Additional tasks can be added here  
                Assert.IsTrue(result);  
            });  

            EnqueueTestComplete();  
        }  
    }  
}  

但我想我不需要EnqueueCallback()的东西。

以下是没有lambdas的代码:

[TestClass]
public class IdentityEditDatabaseTest : WorkItemTest
{
  [TestMethod, Asynchronous] public void testNullInsert()
  {
    wipeTestData(testNullInsertContinue1);
  }
  private void testNullInsertContinue1(String errorString)
  {
    IdentityProperties properties = new IdentityProperties(getContext());
    properties.setUserName(DATABASE_TEST);
    postUserEdit(properties, testNullInsertContinue2);
  }
  private void testNullInsertContinue2(String errorString)
  {
    Assert.assertTrue(errorString == null);

    wipeTestData(testNullInsertContinue3);
  }
  private void testNullInsertContinue3(String errorString)
  {
    TestComplete();
  }
}

...

同样,问题是:

如何使用lambdas(或匿名方法?)将上述字符串串联在一起,这样我就不必为延续语定义命名函数了?

请尽可能详细解释新语法,因为我仍然试图围绕这个概念。

非常感谢!

1 个答案:

答案 0 :(得分:1)

如果我们有以下方法:

private void DoSomething(object argument)
{
    // Do something with the argument here
}

你可能知道它可以像这样分配给一个委托变量:

Action<object> asDelegate = DoSomething;

使用匿名方法进行相同的赋值,我们可以使用lambda表达式:

Action<object> asDelegate = (object argument) =>
    {
        // Do something with the argument here
    }

因此在您的示例中,方法testNullInsert可以这样写:

[TestMethod, Asynchronous]
public void testNullInsert()
{
    wipeTestData((string errorString) =>
    {
        IdentityProperties properties = new IdentityProperties(getContext());
        properties.setUserName(DATABASE_TEST);
        postUserEdit(properties, testNullInsertContinue2);
    });
}

我所做的全部用名称testNullInsertContinue1替换为包含相同功能的lambda表达式。如果你愿意,也可以使用testNullInsertContinue2做同样的事情。

一旦你越来越熟悉使用lambda表达式,你可以删除参数周围的括号(如果只有一个参数)和参数的类型,因为编译器通常可以推断它们,但我已经写了它像这样试着让你尽可能地了解正在发生的事情。希望这会有所帮助。