开发者

How can I write the following callback continuations in c# lambda syntax?

开发者 https://www.devze.com 2023-04-06 06:18 出处:网络
I\'m writing an asynch unit test and I would like to string it together using lambdas (or anonymous methods?) so I don\'t have to define named functions for the continuations.

I'm writing an asynch unit test and I would like to string it together using lambdas (or anonymous methods?) so I don't have to define named functions for the continuations.

I have read several posts on lambdas, but most of these deal with for each style constructs which I am not interested in.

I would like to do something like the following (taken from 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();  
        }  
    }  
}  

But I guess I don't need the EnqueueCallback() stuff.

The following is my code without 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();
  }
}

...

Again, the question is:

How can I string the above together using lambdas (or anonymous methods?) so I don't have to define named functions for the continuations?

Please explain the new syntax as much as possible, as I'm still trying to wrap my head around the concept.

Many thanks!


If we have the following method:

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

you're probably aware that it can be assigned to a delegate variable like this:

Action<object> asDelegate = DoSomething;

to make this same assignment using an anonymous method, we can use a lambda expression:

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

So in your example, the method testNullInsert could be written like this:

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

All I've done there is replaced the name testNullInsertContinue1 with a lambda expression containing the same functionality. You could do the same thing with testNullInsertContinue2 as well if you want.

Once you get more familiar with using lambda expressions, you can drop things like the brackets around the argument (if there's only one argument) and the types of the arguments as the compiler can often infer them, but I've written it like this to try to give you as good an idea of what's going on as possible. Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号