I am loading an assembly and calling a static method that will create a new object of type “MyClass1” (this type is specified at runtime) through reflection using MethodInfo
Another possibility is to write an extension method to this purpose:
public static Task Convert(this Task task) { TaskCompletionSource res = new TaskCompletionSource(); return task.ContinueWith(t => { if (t.IsCanceled) { res.TrySetCanceled(); } else if (t.IsFaulted) { res.TrySetException(t.Exception); } else { res.TrySetResult(t.Result); } return res.Task; } , TaskContinuationOptions.ExecuteSynchronously).Unwrap(); }
It is none-blocking solution and will preserve original state/exception of the Task.