Пример #1
0
        public static object GetResult(this Task task)
        {
            var taskType = task.GetType();

            if (ReferenceEquals(taskType, typeof(Task)))
            {
                return(null);
            }
            else if (
                (taskType.IsGenericType() && taskType.GetGenericTypeDefinition() == typeof(Task <>)) ||
                AsyncStateMachineBoxAccessor.IsAsyncStateMachineBox(taskType) ||
                (taskType.BaseType.IsGenericType() && taskType.BaseType.GetGenericTypeDefinition() == typeof(Task <>)))
            {
#warning pre-compile accessor for Task.GetResult
                var pi     = taskType.GetProperty(nameof(Task <object> .Result));
                var result = pi.GetValue(task);
                if (result != null && result.GetType() == VoidTaskResultType)
                {
                    return(null);
                }
                return(result);
            }
            else
            {
                throw new Exception($"Unknown Task type '{taskType}'.");
            }
        }
Пример #2
0
        public static Type GetTaskResultType(Type taskType)
        {
            if (ReferenceEquals(taskType, typeof(Task)))
            {
                return(VoidTaskResultType);
            }
            else if (taskType.IsGenericType() && taskType.GetGenericTypeDefinition() == typeof(Task <>) ||
                     AsyncStateMachineBoxAccessor.IsAsyncStateMachineBox(taskType))
            {
                return(taskType.GetGenericArguments()[0]);
            }
#warning temporary quick-fix. Still needed? WhenAllPromise : Task<VoidTaskResult>
            else if (taskType.FullName == "System.Threading.Tasks.Task+WhenAllPromise")
            {
                return(VoidTaskResultType);
            }
#warning temporary quick-fix. Still needed? WhenAllPromise<T> : Task<T[]>
            else if (taskType.IsGenericType() && taskType.GetGenericTypeDefinition().FullName == "System.Threading.Tasks.Task+WhenAllPromise`1")
            {
                var elementType = taskType.GetGenericArguments()[0];
                return(elementType.MakeArrayType());
            }
#warning temporary quick-fix. Still needed? DelayPromise : Task<VoidTaskResult>
            else if (taskType.FullName == "System.Threading.Tasks.Task+DelayPromise")
            {
                return(VoidTaskResultType);
            }
            else
            {
                throw new Exception($"Failed to match type [{taskType}] as a known 'Task' type.");
            }
        }