From daa5900f9c6cc2dbc7b1673daac576f1ec15d056 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 1 Jun 2012 16:02:00 +0200 Subject: [PATCH] Fixed async/await decompilation when the GetAwaiter() is called on a value type. --- ICSharpCode.Decompiler/ILAst/AsyncDecompiler.cs | 5 +++++ ICSharpCode.Decompiler/Tests/Async.cs | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/ICSharpCode.Decompiler/ILAst/AsyncDecompiler.cs b/ICSharpCode.Decompiler/ILAst/AsyncDecompiler.cs index 5876febcd..4535c0f82 100644 --- a/ICSharpCode.Decompiler/ILAst/AsyncDecompiler.cs +++ b/ICSharpCode.Decompiler/ILAst/AsyncDecompiler.cs @@ -576,6 +576,11 @@ namespace ICSharpCode.Decompiler.ILAst if (!(getAwaiterCall.Match(ILCode.Call, out getAwaiterMethod, out awaitedExpr) || getAwaiterCall.Match(ILCode.Callvirt, out getAwaiterMethod, out awaitedExpr))) return false; + if (awaitedExpr.Code == ILCode.AddressOf) { + // remove 'AddressOf()' when calling GetAwaiter() on a value type + awaitedExpr = awaitedExpr.Arguments[0]; + } + // brtrue(IL_7C, call(valuetype [mscorlib]System.Runtime.CompilerServices.TaskAwaiter`1::get_IsCompleted, ldloca(CS$0$0001))) ILLabel label; ILExpression getIsCompletedCall; diff --git a/ICSharpCode.Decompiler/Tests/Async.cs b/ICSharpCode.Decompiler/Tests/Async.cs index e4707059c..ccd122255 100644 --- a/ICSharpCode.Decompiler/Tests/Async.cs +++ b/ICSharpCode.Decompiler/Tests/Async.cs @@ -76,6 +76,16 @@ public class Async } } + public async void StreamCopyToWithConfigureAwait(Stream destination, int bufferSize) + { + byte[] array = new byte[bufferSize]; + int count; + while ((count = await destination.ReadAsync(array, 0, array.Length).ConfigureAwait(false)) != 0) + { + await destination.WriteAsync(array, 0, count).ConfigureAwait(false); + } + } + public async void AwaitInLoopCondition() { while (await this.SimpleBoolTaskMethod())