当前位置: 首页>>代码示例>>C#>>正文


C# Task.GetType方法代码示例

本文整理汇总了C#中System.Threading.Tasks.Task.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Task.GetType方法的具体用法?C# Task.GetType怎么用?C# Task.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.Tasks.Task的用法示例。


在下文中一共展示了Task.GetType方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: HandleCompleted

        protected IObservable<object> HandleCompleted(Task task)
        {
            var resultProperty = task.GetType().GetTypeInfo().GetDeclaredProperty("Result");
            
            if (resultProperty != null)
            {
                switch (task.Status)
                {
                    case TaskStatus.RanToCompletion:
                        return Observable.Return(resultProperty.GetValue(task));
                    case TaskStatus.Faulted:
                        return Observable.Return(new BindingNotification(task.Exception, BindingErrorType.Error));
                    default:
                        throw new AvaloniaInternalException("HandleCompleted called for non-completed Task.");
                }
            }

            return Observable.Empty<object>();
        }
开发者ID:mikel785,项目名称:Perspex,代码行数:19,代码来源:TaskValuePlugin.cs

示例2: GetTaskInfo

        private static Tuple<object, Type> GetTaskInfo(Task task)
        {
            Type taskType = task.GetType();

            bool isTypedTask = taskType.IsGenericTask();
            bool isVoidTask = !isTypedTask && typeof(Task).IsAssignableFrom(taskType);

            if (!isTypedTask && !isVoidTask)
            {
                throw new InvalidOperationException(Resources.Global.InvalidIAsyncResultReturned);
            }

            if (isVoidTask)
            {
                return new Tuple<object, Type>(null, typeof(void));
            }

            Type taskResultType = taskType.GetGenericArguments()[0];
            var dynamicTask = (dynamic) task;

            return new Tuple<object, Type>(dynamicTask.Result, taskResultType);
        }
开发者ID:dstarosta,项目名称:GitProjects,代码行数:22,代码来源:ServiceMethodInvoker.cs

示例3: GetResult

 public static object GetResult(Task task) => task.GetType() == typeof(Task) ? null : ((dynamic)task).Result;
开发者ID:darilek,项目名称:dotvvm,代码行数:1,代码来源:TaskUtils.cs

示例4: GetTaskResult

 private object GetTaskResult(Task task)
 {
     return task.GetType().GetProperty("Result").GetValue(task);
 }
开发者ID:Ontropix,项目名称:CQRSalad,代码行数:4,代码来源:UnitTest1.cs

示例5: GetTaskResult

        /// <summary>
        /// get result of task
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        private static object GetTaskResult(Task task)
        {
            try
            {
                var type = task.GetType();
                var func = GetTaskResultFuncs.GetOrAdd(type, (Type key) =>
                {
                    var arg = Expression.Parameter(typeof(object), "task");
                    var argConvert = Expression.Convert(arg, type);
                    var expProp = Expression.Property(argConvert, "Result");
                    var expConvert = Expression.Convert(expProp, typeof(object));
                    var asignExp = Expression.Lambda(expConvert, arg);
                    var getFunc = asignExp.Compile();
                    return (Func<object, object>)getFunc;
                });

                var value = func?.Invoke(task);
                return value;
            }
            catch (Exception ex)
            {
                LogHelper.Error("GetTaskResult error", ex);
                var aex = ex as AggregateException;
                if (aex?.InnerException != null)
                    throw aex.InnerException;
                throw;
            }
        }
开发者ID:chrishaly,项目名称:RpcLite,代码行数:33,代码来源:RpcAction.cs

示例6: InvokeAsync_IfLambdaReturnsNonGenericTask_ReturnsCompletedTask

        public void InvokeAsync_IfLambdaReturnsNonGenericTask_ReturnsCompletedTask()
        {
            // Arrange
            Func<object, object[], Task> lambda = (i1, i2) =>
            {
                Task innerTask = new Task(() => { });
                innerTask.Start();
                Assert.False(innerTask.GetType().IsGenericType); // Guard
                return innerTask;
            };

            IMethodInvoker<object> invoker = CreateProductUnderTest(lambda);
            object instance = null;
            object[] arguments = null;

            // Act
            Task task = invoker.InvokeAsync(instance, arguments);

            // Assert
            Assert.NotNull(task);
            task.WaitUntilCompleted();
            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
        }
开发者ID:rafaelmtz,项目名称:azure-webjobs-sdk,代码行数:23,代码来源:TaskMethodInvokerTests.cs

示例7: GetTaskResult

		private static object GetTaskResult(Task task)
		{
			var taskType = task.GetType();
			var typeInfo = taskType.GetTypeInfo();
			if (!typeInfo.IsGenericType) { 
				throw new SomehowRecievedTaskWithoutResultException();
			}

			var resultProperty = typeInfo.GetDeclaredProperty("Result").GetMethod;
			return resultProperty.Invoke(task, new object[] { });
		}
开发者ID:Sandboxed-Forks,项目名称:Enexure.MicroBus,代码行数:11,代码来源:PipelineBuilder.cs


注:本文中的System.Threading.Tasks.Task.GetType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。