2011-09-21 62 views
1

我正在編寫一個異步單元測試,我想用lambdas(或匿名方法?)將它們串在一起,所以我不必爲延續定義命名函數。如何在C#lambda語法中編寫以下回調延續?

我已經lambda表達式讀的幾個職位,但thesemost處理每個樣式結構,我沒興趣

我願做類似如下(從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()的東西。

以下是我的代碼,而lambda表達式:

[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(); 
    } 
} 

... 

同樣,問題是:

我如何串用上面在一起的lambda(?或匿名方法),所以我不知道必須爲繼續定義命名函數?

請儘可能地解釋新的語法,因爲我仍然試圖圍繞這個概念進行思考。

非常感謝!

+0

我可以是完全愚蠢的,也許我不明白這個問題(它看起來很難),但你爲什麼不訂閱timer.Tick事件? –

回答

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 
    } 

所以在你的例子中,方法tes tNullInsert可以這樣寫:

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

所有我已經有更換名稱testNullInsertContinue1包含相同的功能lambda表達式來完成。如果你願意,你也可以用testNullInsertContinue2來做同樣的事情。

一旦你對使用lambda表達式更加熟悉,你可以在參數(如果只有一個參數)和參數的類型(如編譯器經常可以推斷出它們)上放置類似括號的東西,但是我寫了它是這樣試圖給你一個儘可能好的想法。希望這可以幫助。

+0

謝謝,這完美地回答了我的問題。不幸的是我不能使用這種方法,因爲這意味着我需要重新命名變量「errorString」,每次鏈接lambda時避免CS0136 :( – swinefeaster