示例#1
0
        public static Task AsGenericTaskException(this Exception exception, Type type)
        {
            var tcs = TaskCompletionSourceGeneric.Create(type);

            tcs.SetException(exception);
            return(tcs.Task);
        }
示例#2
0
        public static Task AsGenericTaskResult(this object result, Type type)
        {
            var tcs = TaskCompletionSourceGeneric.Create(type);

            tcs.SetResult(result);
            return(tcs.Task);
        }
示例#3
0
        public static Task CastResultAs <T>(this Task <T> task, Type resultTypeToCastTo)
        {
            var tcs = TaskCompletionSourceGeneric.Create(resultTypeToCastTo);

            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    tcs.SetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    tcs.SetCanceled();
                }
                else
                {
                    try
                    {
                        tcs.SetResult(t.Result);
                    }
                    catch (InvalidCastException ex)
                    {
                        tcs.SetException(ex);
                    }
                }
            }, TaskContinuationOptions.ExecuteSynchronously);
            return(tcs.Task);
        }
示例#4
0
 public static TaskCompletionSourceGeneric AsTCSGeneric(this Type type, object state) => TaskCompletionSourceGeneric.Create(type, state);