且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

任务<>不包含'GetAwaiter“的定义

更新时间:2021-11-14 15:58:11

GetAwaiter(),所使用的等待,被实现为在异步CTP的扩展方法。我不知道你到底是使用的是什么(你在你的问题提两个异步CTP和VS 2012 RC),但它是可能的异步针对包使用同样的技术。

GetAwaiter(), that is used by await, is implemented as an extension method in the Async CTP. I'm not sure what exactly are you using (you mention both the Async CTP and VS 2012 RC in your question), but it's possible the Async targeting pack uses the same technique.

问题则是,扩展方法不动态工作。你可以做的是要明确指定你与工作,这意味着扩展方法将工作的工作,然后再切换回动态

The problem then is that extension methods don't work with dynamic. What you can do is to explicitly specify that you're working with a Task, which means the extension method will work, and then switch back to dynamic:

private async void MyButtonClick(object sender, RoutedEventArgs e)
{
    dynamic request = new SerializableDynamicObject();
    request.Operation = "test";

    Task<SerializableDynamicObject> task = Client(request);
    dynamic result = await task;

    // use result here
}

或者,因为客户端()方法实际上不是不是动态的,可以用 SerializableDynamicObject 调用它,没有动态等限制使用动态尽可能的:

Or, since the Client() method is not actually not dynamic, you could call it with SerializableDynamicObject, not dynamic, and so limit using dynamic as much as possible:

private async void MyButtonClick(object sender, RoutedEventArgs e)
{
    var request = new SerializableDynamicObject();
    dynamic dynamicRequest = request;
    dynamicRequest.Operation = "test";

    var task = Client(request);
    dynamic result = await task;

    // use result here
}