-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solves #37
- Loading branch information
Showing
3 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Activout.RestClient/Helpers/Implementation/TaskConverter3.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
|
||
namespace Activout.RestClient.Helpers.Implementation; | ||
|
||
/* | ||
* Convert from Task<object> to Task<T> where T is the Type | ||
* | ||
* Implemented by creating a TaskCompletionSource<T> and setting the result or exception | ||
*/ | ||
public class TaskConverter3<T> : ITaskConverter | ||
{ | ||
[StackTraceHidden] | ||
public object ConvertReturnType(Task<object> task) | ||
{ | ||
return ConvertReturnTypeImpl(task); | ||
} | ||
|
||
[StackTraceHidden] | ||
private static async Task<T> ConvertReturnTypeImpl(Task<object> task) | ||
{ | ||
var taskCompletionSource = new TaskCompletionSource<T>(); | ||
try | ||
{ | ||
taskCompletionSource.SetResult((T)await task); | ||
} | ||
catch (Exception e) | ||
{ | ||
taskCompletionSource.SetException(e); | ||
} | ||
|
||
return await taskCompletionSource.Task; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Activout.RestClient/Helpers/Implementation/TaskConverter3Factory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace Activout.RestClient.Helpers.Implementation; | ||
|
||
public class TaskConverter3Factory : ITaskConverterFactory | ||
{ | ||
public ITaskConverter CreateTaskConverter(Type actualReturnType) | ||
{ | ||
return actualReturnType == typeof(void) ? null : | ||
(ITaskConverter) Activator.CreateInstance(typeof(TaskConverter3<>).MakeGenericType(actualReturnType)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters